IOError:[Errno 2]没有这样的文件或目录:' 200'

时间:2015-03-26 15:01:09

标签: python ioerror

我正在尝试从目录中读取所有文件并拆分单词。 但在读完第一个文件后,我收到错误IOError:[Errno 2]没有这样的文件或目录:'200'。 这是我的代码:

#!/usr/bin/env python
import sys
import os
mypath = 'data/'
listing = os.listdir(mypath)
print listing
for infile in listing:
        print "current file is: " + infile
        with open(infile,'r') as inputd:
                for line in inputd:
                        line = line.strip()
                        words = line.split()
                        for word in words:
                                print '%s' % (word)

目录包含文件100,200,300和400 我正在输出

~$ python mapper3.py
['100', '200', '300', '400']
current file is: 100
opened:100
100 1
200 1
217 1
300 1
400 1
current file is: 200
opened:200
Traceback (most recent call last):
  File "mapper3.py", line 13, in <module>
    with open(infile,'r') as inputd:
IOError: [Errno 2] No such file or directory: '200'

1 个答案:

答案 0 :(得分:0)

您正在尝试打开名为&#34; 200&#34;的文件。在当前的工作目录中,而不是data/中的那个。尝试指定完整路径。

with open(os.path.join(mypath, infile),'r') as inputd: