How to access separate python file in another python replit to store data like a data frame

**Question:**I am writing student grading program it works but I want to store results in seperate file in another replit program file like a data frame. how does first replit progam access second to do this? Reason I need another second file storing program is because students will take test at seperate times so I need to store each result in seperate file to get total for whole class.


code snippet

Can you explain your situation better, I cannot understand.

2 Likes

Could you be a bit more clear and elaborate a little bit (for instance what exactly do you mean by ā€œdataframeā€)?
If youā€™ve made a Repl for this and started on it, share it as well.

1 Like

Hey, @panagranit welcome to the forums!

Can you please provide a link to the repl? This way it is easier for staff and members of the community to help you!

Also see this guide on how to share your code:

1 Like

I assume what you want is to save the output into a file, or multiple files
You can use with open(file name, ā€˜wā€™) as f: f.write(output)
however I still do not understand what you want exactly

1 Like

I am non sure it is possible to import a file in a REPL from another repl without going throught a GIT, copy&paste he file or some overly complicated always-on server.
Would be great thought is possible

2 Likes

https://replit.com/@panagranit/quiz-questions?v=1

https://replit.com/@panagranit/quiz-questions?v=1
I want to put student_answers in to file that is not current directory. the student_answers will go in
a file that will be a python program that will take student answers and put in dataframe python

Hello @panagranit if you have a function in a separate python file, you can run this code:

Hello_World.py: The code that needs to be accessed:

def Hello_World():
    print("Hello World")

Main.py: The Code that will run the Hello_World function (in another python file):

from Hello_World import Hello_World #Imports the Hello_World Function
Hello_World()

Output:

Hello World

I hope this helps!

@MilesWK

1 Like

Is the file Main.py in same directory as Hello_World? How to access files not in your main program directory like one you created months ago on something else ? the file is replit file.

You canā€™t do that in Replit.

well you can link it but can you make it run in the link as a separate program.? in other words do you start a program on one replit page and then it references another Replit page URL can you make that second one do something with data from the first?

OK so I see a solution to my problem. Because the students are taking the test at different times what Iā€™ll do is store it students answer in a text file. When they finish the program their answers will be stored in a text file. Then the next student can take the test asynchronously and put their answers in and it will be stored as a text file at the end of the day or whatever I should have multiple text files with different names associated with them that I can do an analysis on. I donā€™t have to get a separate replit webpage for that. hopefully the python program can create a new text file to store the answers and each time without it being it being erased every time when you start the pram up.

While I do not understand your most recent comment, The python file does NOT need to be in the same directory. For example if the hello_world.py file is in a folder called ā€œprogram files,ā€ (the folder is in the same directory as the main.py file) you would do it like this (I think):

from program_files/hello_world import hello_world

I will need to test this. I will get back to you when I do!

Correct me if Iā€™m wrong but thatā€™s invalid syntax.

Oh shoot, you are right. Let me brainstorm this as I havenā€™t done this yetā€¦ hold on!

based on my knowledge, underscores arenā€™t possible in python library names and if a library is uploaded to pypi with an underscore it will be converted to a hyphen.

1 Like

Okay, I brainstormed and this is how to do it with the hello_world file in Program Files folder:

from Program_Files import hello_world 
hello_world.hello_world()

See my code here:
https://replit.com/@MilesWK/Getting-Programs-From-Another-FileFolder?v=1
image

1 Like

For shorthand I think you can change it to this:

from Program_Files import hello_world as hw
hw.hello_world()

That would work. I didnā€™t do that for clarity of showing the program, but you are 100% correct.

1 Like