2021年3月6日星期六

Why is open('file','w') saying file doesn't exist?

I have a function:

def loadFile(file):      path = script_dir + file #script_dir has the form '/home/username/blah/blah/' and file has the form 'asdf/foo.txt'      temp = open(path,'w')      temp.close()      with open(path) as f:          return f.read().replace('\n', '')  

When I run print(loadFile('asdf/asdf.txt')) in python3, I get the error:

FileNotFoundError: [Errno 2] No such file or directory: '/home/username/blah/blah/asdf/asdf.txt'  

Why is this happening? I am very confused. everywhere I look it says that 'w' will create the file, why isn't it.

Note1: I run Zorin OS Education Lite (sorry I can't provide you with more universal terminology).
Note2: I have an NTFS partition mounted at /home/username/blah/

https://stackoverflow.com/questions/66510858/why-is-openfile-w-saying-file-doesnt-exist March 07, 2021 at 05:37AM

没有评论:

发表评论