1

I have the following code:

for file in os.listdir('/home/sainik/Final/'+str(folderno)):
        if file.endswith('.csv'):
            print file
            with open(file,'rb') as csvfile:
                spamreader = csv.reader(csvfile)
                for row in spamreader:
                    print row        

when running the code, I am getting the following error:

Traceback (most recent call last):
  File "/home/sainik/Final/Programs/sainik.py", line 28, in <module>
    with open(file,'rb') as csvfile:
IOError: [Errno 2] No such file or directory: '4.csv'

Kindly help.

3 Answers 3

1

You are trying to open the file from the path you are running the script.

You should try to open the full path

with open('/home/sainik/Final/' + file)
Sign up to request clarification or add additional context in comments.

Comments

1

You are passing only the filename for open function. You should pass path to the open function. Two possible ways to pass the path of file to open function, either relative path or full path.

try:

with open( os.path.join('/home/sainik/Final/',str(folderno),file),'rb') as csvfile:

Comments

0

Your script is looking at it's own directory for file 4.csv. Try it like this:

for file in os.listdir('/home/sainik/Final/'+str(folderno)):
        if file.endswith('.csv'):
            print file
            with open(/home/sainik/Final/'+str(folderno)+'\/'+file,'rb') as csvfile:
                spamreader = csv.reader(csvfile)
                for row in spamreader:
                    print row  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.