【问题标题】:IOError: [Errno 2] No such file or directory, even though the file is existedIOError: [Errno 2] 没有这样的文件或目录,即使该文件存在
【发布时间】:2015-05-17 16:59:51
【问题描述】:

我想弄清楚为什么会出现这样的错误。我为另一个包含四个文件的目录运行了完全相同的代码,它工作得很好。这次使用另一个目录我收到错误这个错误

IOError: [Errno 2] No such file or directory:

即使文件存在。这是适用于一个目录但不是另一个目录的代码,两个目录都存在,因此它们的四个文件

行错误:"with open((file_name),'r') as f:"

import sys,csv,os
d_files = {}
def Readfile(file_name):
    d_files[file_name] = []
    print "file_name", file_name # printing the right name 
    with open((file_name),'r') as f:
             reader=csv.reader((f),delimiter='\t')
             for row in reader:
              d_files[file_name].append(row)
print
try:
    folder_input = raw_input("Please enter you folder name containing 4 files:   ")
except Name_Error:
    pass
for root,dirs,files in os.walk(folder_input):
for file in files:
    print "file",file  # the right file name 
    pathname=os.path.join(root,file)
    print "DIR:  ",pathname  # right directory inputted 
    print "Now, the file is being parsed"
    Readfile(file)
    print "Now, file", file, "is done parsed"
    print

用户将键入四个文件的路径,我在一个目录中测试过它,但它对另一个目录无效,我 100% 确定路径正确且文件存在。

提前非常感谢

【问题讨论】:

  • 您应该使用您正在使用 os.path.join(root,file) 格式化的路径名调用 Readfile。我的意思是调用像 Readfile(pathname)

标签: python filehandler


【解决方案1】:

改为使用路径名调用 Readfile。如下图:

import sys,csv,os
d_files = {}
def Readfile(file_name):
    d_files[file_name] = []
    print "file_name", file_name # printing the right name 
    with open((file_name),'r') as f:
             reader=csv.reader((f),delimiter='\t')
             for row in reader:
              d_files[file_name].append(row)
print
try:
    folder_input = raw_input("Please enter you folder name containing 4 files:   ")
except Name_Error:
    pass
for root,dirs,files in os.walk(folder_input):
for file in files:
    print "file",file  # the right file name 
    pathname=os.path.join(root,file)
    print "DIR:  ",pathname  # right directory inputted 
    print "Now, the file is being parsed"
    Readfile(pathname)
    print "Now, file", file, "is done parsed"
    print

【讨论】:

  • 非常感谢它确实解决了这个问题,但我的问题是为什么它适用于一个目录而不适用于另一个目录?
【解决方案2】:

尝试以下方法:

import sys,csv,os
d_files = {}
def Readfile(file_name):
    d_files[file_name] = []
    print "file_name", file_name # printing the right name 
    with open(file_name,'r') as f:
             reader=csv.reader((f),delimiter='\t')
             for row in reader:
                 d_files[file_name].append(row)
print
try:
    folder_input = raw_input("Please enter you folder name containing 4 files: ")
except Name_Error:
    pass
for root,dirs,files in os.walk(folder_input):
    for file in files:
        print "file",file  # the right file name 
        pathname=os.path.join(root,file)
        print "DIR:  ",pathname  # right directory inputted 
        print "Now, the file is being parsed"
        # Make sure here you type a file name under same directory
        # or full path: "C:\\boot.ini" or "/etc/passwd". Also make sure the user running the script has permission for the folder.
        Readfile(file)
        print "Now, file", file, "is done parsed"
        print

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多