0

I have kept csv file in D directory and trying to read that csv file using python programming. This is my code in python:

import csv

try:
     path = "D:\\abc.csv"
     with open("path", "rb") as csvfile:
         readCSV = csv.reader(csvfile, delimiter=',')
         for row in readCSV:
             print (row)    
except Exception, e:
    raise e

This is the error I am getting:

The current working directory is F:\
Traceback (most recent call last):
  File "F:\directory.py", line 16, in <module>
    raise e
IOError: [Errno 2] No such file or directory: 'path'
1

3 Answers 3

1

Remove the double quotes around path. Edit it to this:

with open(path, "rb") as csvfile:
Sign up to request clarification or add additional context in comments.

Comments

1

You need to remove the double quotes around path. "path" is a string and in-order to get the value of path variable , you need to put the variable as it is, so that it got expanded.

with open(path, "rb") as csvfile:

Comments

1

This line

with open("path", "rb") as csvfile: 

should be

with open(path, "rb") as csvfile:

As any text in "" or '' in python would be consider as string type instead of variable.

2 Comments

I have removed the double quotes around the path. Now i am getting following error: The current working directory is F:\ Traceback (most recent call last): File "F:\directory.py", line 16, in <module> raise e AttributeError: 'module' object has no attribute 'reader'
I think you have named the python script as csv.py Check out this link Please avoid naming .py script with common library names such as csv,os,etc as python will think you are import the .py instead of the library

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.