Python giving FileNotFoundError for file name retu

2020-01-23 13:11发布

I was trying to iterate over the files in a directory like this:

import os

path = r'E:/somedir'

for filename in os.listdir(path):
    f = open(filename, 'r')
    ... # process the file

But Python was throwing FileNotFoundError even though the file exists:

Traceback (most recent call last):
  File "E:/ADMTM/TestT.py", line 6, in <module>
    f = open(filename, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'foo.txt'

So what is wrong here?

2条回答
神经病院院长
2楼-- · 2020-01-23 13:28

os.listdir(directory) returns a list of file names in directory. So unless directory is your current working directory, you need to join those file names with the actual directory to get a proper absolute path:

for filename in os.listdir(path):
    filepath = os.path.join(path, filename)
    f = open(filepath,'r')
    raw = f.read()
    # ...
查看更多
对你真心纯属浪费
3楼-- · 2020-01-23 13:31

It is because os.listdir does not return the full path to the file, only the filename part; that is 'foo.txt', when open would want 'E:/somedir/foo.txt' because the file does not exist in the current directory.

Use os.path.join to prepend the directory to your filename:

path = r'E:/somedir'

for filename in os.listdir(path):
    with open(os.path.join(path, filename)) as f:
        ... # process the file

(Also, you are not closing the file; the with block will take care of it automatically).

查看更多
登录 后发表回答