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'
user
  • 854
  • 2
  • 12
  • 28
  • 3
    before calling `open()`, check if the file exists [os.path.exists()](http://docs.python.org/2/library/os.path.html#os.path.exists) – chickity china chinese chicken Dec 13 '16 at 01: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: https://stackoverflow.com/questions/2953834/windows-path-in-python – Daniel Porteous Dec 13 '16 at 01:39
  • @downshift: Umm... No. That's an [LBYL](https://docs.python.org/3/glossary.html#term-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](https://docs.python.org/3/glossary.html#term-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. – ShadowRanger Dec 13 '16 at 01: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](https://docs.python.org/3/library/csv.html#id1), 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. – ShadowRanger Dec 13 '16 at 01: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. – ShadowRanger Dec 13 '16 at 01:56
  • @ShadowRanger When I go into the file directory I can visually see the file. It probably has something to do with the permissions as you mentioned. I'm not quite sure how to give the file those access. – user Dec 13 '16 at 01:56
  • @user: Is the Python script running as you? If so, and you can read the directory (without UAC prompts), then the script should be able to as well. On the other hand, if the script is running from some completely different account, then yeah, access issues would be a possibility. In general, if you want a file to be commonly accessible, you don't want to put it in a user-specific directory, because it either won't be accessible to others, or making it accessible might accidentally make other profile data accessible to others. – ShadowRanger Dec 13 '16 at 02:00
  • @ShadowRanger I am running the python script, also I checked the test.csv permissions, it has all the read and write permissions allowed my for system and account. – user Dec 13 '16 at 02:05
  • @user: Also, are you actually looking at that exact path? Your `Desktop` is a "magic" folder; if you just clicked `Desktop` in the Explorer window, it might not be showing you what's at `C:\Users\Sam\Desktop\big data\workspace`, due to the folder magic involved with `Desktop` (admittedly, usually only a concern in a corporately managed environment, but I've customized it myself before). In a normal command windows (`cmd.exe`) try `cd`ing there by the whole path and make sure it has what you expect. Just because you see it on your Desktop doesn't mean it's in that specific folder. – ShadowRanger Dec 13 '16 at 02:05
  • For reference on the magic folders and how you're supposed to work with them, see [this question](http://stackoverflow.com/q/3858851/364696). – ShadowRanger Dec 13 '16 at 02:07

3 Answers3

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/

Ramon Zuniga
  • 305
  • 2
  • 14
  • Most of this is redundant with [this (wrong) answer](http://stackoverflow.com/a/41112409/364696) (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`). – ShadowRanger Dec 13 '16 at 02:40
  • You are right about the raw strings, I bet is the file extension – Ramon Zuniga Dec 13 '16 at 02:41
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.

Community
  • 1
  • 1
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
-2

Edited- try this

path = r"C:/Users/Sam/Desktop/big data/workspace/test.csv"
csvfile = open(path, 'rb')
Illusionist
  • 5,204
  • 11
  • 46
  • 76
  • 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. – ShadowRanger Dec 13 '16 at 01:49