BioInformatician
BioInformatician

Reputation: 97

IOError: [Errno 2] No such file or directory, even though the file is existed

I am trying to figure out why I am having such error. I ran the same exact code for another directory which contains four files and it is working just fine. This time with using another directory I am getting error this error

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

even though the files existed. Here is the code which works fine for one directory but not the other one both directory exists and so their four files

The error at line:"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

The user will type the path of the four files and which I tested for one directory and it worked but not for the other directory which I am 100% sure that the path is correct and the files exist.

Thanks a lot in advance

Upvotes: 0

Views: 1722

Answers (2)

Nipun Talukdar
Nipun Talukdar

Reputation: 5387

Call Readfile with pathname instead. As shown below:

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

Upvotes: 1

Jack Amsteel
Jack Amsteel

Reputation: 21

Try the following:

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

Upvotes: 0

Related Questions