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'
smci
  • 32,567
  • 20
  • 113
  • 146
mohammad khan
  • 51
  • 1
  • 1
  • 9
  • Possible duplicate of [How do I read and write CSV files with Python?](http://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – Martin Thoma Jan 11 '17 at 07:55

3 Answers3

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:
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

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

with open(path, "rb") as csvfile:
agamagarwal
  • 978
  • 7
  • 17
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.

Alex Fung
  • 1,996
  • 13
  • 21
  • 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 raise e AttributeError: 'module' object has no attribute 'reader' – mohammad khan Apr 02 '15 at 07:12
  • 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 – Alex Fung Apr 02 '15 at 08:35