18F / an_introduction_to_python

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

Homework 2 question about directories #6

Open JosephMacula opened 7 years ago

JosephMacula commented 7 years ago

I created the two .py files, the two .txt files and the to_traverse/ directory for this assignment, but 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?