18F / an_introduction_to_python

An introduction to Python - https://www.digitalgov.gov/event/online-intro-to-python/
10 stars 8 forks source link

Questions on HW2 #5

Closed JosephMacula closed 7 years ago

JosephMacula commented 7 years ago

I’m working on the 3 week python class that just wrapped and I’m finishing homework 2. Unfortunately, I have to use pythonanywhere to do the work, since I don’t have administrative privileges to install software on my work computer. I have just a few questions.

I opened up a “console” on pythonanywhere (which I’m assuming is sort of a python-specific CLI?) and wrote the following code: new_file = open(“create_file.py”, “w”). I know that this command will create a new file for me called create_file and that it will be stored in the current working directory (which from what I can tell is home/jmacula for me). However, after hitting enter and getting a new line in the console, I’m not sure how I can write to create_file.py the code for a program that will create another¬ file (in this case, practicing_writing_text.txt). What I did to get around this problem was to just go to my dashboard and click on the file that I just created, create_file.py, and then write the code needed to create practicing_writing_text.txt in that file. Is that how we were supposed to do this? I’m not sure if I’m missing something important here.

Once I created the two .py files, the two .txt files (for edit.py and practicing_file_updating.txt I just followed the method described above), and the to_traverse/ directory, I’m getting stuck on moving the .txt files into the to_traverse directory using code. I tried using both the os.rename() and the shutil.move() methods as follows:

os.rename(home/jmacula/practicing_file_writing.txt, home/jmacula/to_traverse/practicing_file_writing.txt)
os.rename(“home/jmacula/practicing_file_writing.txt”, “home/jmacula/to_traverse/practicing_file_writing.txt”)

shutil.move(home/jmacula/practicing_file_writing.txt, home/jmacula/to_traverse/practicing_file_writing.txt)
shutil.move(“home/jmacula/practicing_file_writing.txt”, “home/jmacula/to_traverse/practicing_file_writing.txt”)

When I don’t wrap the file paths in quotes, python throws me the following error: NameError: name ‘practicing_file_writing’ is not defined. When I wrap them in quotes, python throws me a different error: FileNotFoundError: [Errno 2] No such file or directory ‘practicing_file_writing.txt’. Could you explain briefly (I know you’re busy and have limited time for newbies!) how you’re supposed to use these methods?

For the last bit of the homework, I’m assuming I should do the following: import os, then change the cwd to whatever directory is above to_traverse/ (which I’m assuming is jmacula), create a .py file there, then open that .py file and write the necessary code to change directories, read in the .txt files, assign the contents of those read commands to variables, then compare those variables to test for equality?

Sorry if this should all be super obvious; I looked through some SO questions/python documentation, but couldn’t get satisfactory answers to these questions.

EricSchles commented 7 years ago

Can you break these out into specific questions? This is going to be a bit overwhelming to people looking for quick answers.