What is happening when I change directory and then run Python?

时间:2018-09-18 20:15:47

标签: python

I have looked at so many posts, and I am still struggling to grasp this.

testfile.txt is in the /home/myusername folder

$ pwd
/home/myusername

$ python3

>>> open('testfile.txt')
#Success

>>> quit()

$ cd folderA
$ python3
>>> open('testfile.txt')
#No such file

// Move testfile to folderA
>>> open('testfile.txt')
#No such file

Try to change directory,
>>> import os
>>> os.chdir("folderA")
>>> open('testfile.txt')
#fail, no such file

So, I'm just wondering what's going on here... 1) When I change directory the Python interpreter seems to not find my file in the directory I changed to OR even in the home/username directory where it was working before I changed directory!! 2) If I use os to change directory to a folder this seems to do nothing for me.

So, if my steps are correct, changing directory in terminal and then running python seems to upset where python's relative path is.. and os.chdir doesn't seem to help.

FYI: I am running Ubuntu (Windows) from Command Prompt, and even when I try absolute path '/home/username/folder/file.txt' it still isn't working. This is the best I can do... Please help!

0 个答案:

没有答案