IOError:[Errno 2]没有这样的文件或目录,即使该文件存在

时间:2015-05-17 08:46:40

标签: python filehandler

我想知道为什么我会遇到这样的错误。我为另一个包含四个文件的目录运行了相同的代码,它运行正常。这次使用另一个目录我收到错误此错误

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

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

行中的错误:“打开((file_name),'r')为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%确定路径正确且文件存在的其他目录。

提前多多感谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试以下方法:

<form action="formulari.php" method="post">
    <p>Nombre:<input name= "name" type="text"></p>
    <p>email:<input name= "email"  type="text"></p>
    <p>Comentario:
        <textarea name= "comentari" rows="4" cols="50">
        </textarea>
    </p>
    <input type="submit" value="enviar" >
    <input type="reset" value="borrar" >
 </form>