3

I am trying to open a csv file and load it, however it states that it cannot find the file, when it clearly exists in the file path i wrote it to be.

Code

#Load the CSV file into CSV reader
csvfile = open("C:/Users/Sam/Desktop/big data/workspace/test.csv",'rb')

Error

Traceback (most recent call last):
  File "C:/Users/Sam/Desktop/big data/workspace/yelpdatabase.py", line 16, in <module>
    csvfile = open("C:/Users/Sam/Desktop/big data/workspace/test.csv",'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Sam/Desktop/big data/workspace/test.csv'
10
  • 3
    before calling open(), check if the file exists os.path.exists() Commented Dec 13, 2016 at 1:31
  • As @downshift said, you want to check that the file exists. As another suggestion, you should try to use os.path.join to more reliably build the file path, as in the answer to this question here: stackoverflow.com/questions/2953834/windows-path-in-python Commented Dec 13, 2016 at 1:39
  • @downshift: Umm... No. That's an LBYL pattern which is subject to race conditions (test says it exists, something deletes it between test and open) and wasted time on the stat check, when open itself implicitly checks existence. Correct solution is EAFP: call open, catch OSError (or subclasses thereof in modern Python, e.g. FileNotFoundError in this case) if you can handle the error. os.path.exists is fine for interactive interpreter debugging, but not production code. Commented Dec 13, 2016 at 1:42
  • Side-note: In general, on Python 3, you don't open CSV files in binary mode. You open them in normal text mode, with newline='' to disable line ending translation, so the csv module can handle line ending translation correctly per the CSV dialect in use. Opening in binary mode was how you handled this in Python 2, because Python 2 wasn't unicode friendly, so binary mode avoided decoding and newline translation at once. Commented Dec 13, 2016 at 1:51
  • 2
    How are you so sure the file exists at that exact path? And that your program is running with permissions to read it? Python sometimes has some bugs in terms of how it reports access denied errors on Windows IIRC, so I'd suspect this is either a non-existent file, or the program lacks read access. Commented Dec 13, 2016 at 1:56

3 Answers 3

2

Try like this:

csvfile = open(r"C:/Users/Sam/Desktop/big data/workspace/test.csv","rb")

Make sure your file name is not test.csv.txt sometimes windows takes .csv as part of the filename

Tested using Python 2.7.12

Checkout http://shortcode.pro/code/open-csv-file-with-python/

Sign up to request clarification or add additional context in comments.

2 Comments

Most of this is redundant with this (wrong) answer (raw strings are irrelevant if you're using forward slashes; if there are no backslashes in the string, raw and non-raw literals have identical meaning). The suggestion that file extension hiding might be tripping them up is a good one though (I forgot about it, because the first thing I do on a new Windows systems is disable Hide Extensions for Known File Types).
You are right about the raw strings, I bet is the file extension
1

Posting this since it's a likely cause of problems like this, if not necessarily in this specific case. Desktop (along with most other user folders) is a pseudo-magic folder, and you can't rely on it always being found at C:\Users\USERNAME\Desktop. In particular, just because you see a file on your desktop doesn't mean it's actually there. It could be in the All Users Desktop folder, and either of those could be redirected by the Windows Library folder redirection magic.

If you want to get the correct path to the user's desktop dynamically, you can use the pywin32 extension to do so:

from win32com.shell import shell, shellcon
shell.SHGetFolderPath(0, shellcon.CSIDL_MYPICTURES, None, 0)

(hat tip to this answer), or to get the common Desktop folder for all users:

import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell")
allUserDocs = objShell.SpecialFolders("AllUsersDesktop")

(hat tip to this answer).

Both approaches above might be replaceable on Vista and higher with code that calls SHGetKnownFolderPath (either with pywin32 if it supports it, or directly through a ctypes wrapper) using FOLDERID_Desktop and FOLDERID_PublicDesktop for the user specific and commonly shared Desktop folders.

Comments

-2

Edited- try this

path = r"C:/Users/Sam/Desktop/big data/workspace/test.csv"
csvfile = open(path, 'rb')

1 Comment

Raw strings are good for using backslashed paths, but they're redundant if you're using forward slashes; there are no backslashes to "raw-ify". The OP's non-raw version of this string is going to have the exact same contents.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.