Issues with accessing CSV files using Python


#1

Hello,

I haven’t been using Atom for long but have recently started using it for some basic python and web development. I am working with CSV files on macOS using python version 3.6.1.

My package list is:
file-icons
open-in-browsers
script
atom-beautify
atom-clock
atom-django
atom-terminal
autocomplete-python
color-picker
fold-functions
kite
linter-flake8
python-indent
python-tools

I had a couple of package updates waiting for me on Monday which I installed. When I reopened code which i had written on Sunday and had no problems accessing CSV files (other issues but those are irrelevant). After the package updates I continued with the same code and without making any changes whatsoever I started to get FileNotFoundError when trying to open CSV files.

The exact same code works absolutely fine in IdLE
The CSV file is obiously in the same folder

I have used both:
with open(“example.csv”,“r” as openFile:
and
openFile = open(“example.csv”,“r”)
and both have failed. I have also written basic scripts which just attempt to access the CSV file (which again work in Idle) but don’t work in Atom.
I have disabled the packages and tried again - still not working.

I am completely confused and really do not want to go back to using idle.

Thanks
Brian


#2

Since the issue is with executing Python scripts, not Atom, everything other than script and your code is probably irrelevant. script (by default) ships your code off to whatever executable is behind the python command, so you can check it by opening the command line and trying to execute the same script. Another thing to try would be to use the full path to the csv file and see if that makes a difference.


#3

I’m using a hashbang to run the code in 3.6.1 (otherwise it runs in 2.7). I’m not using virtual environments yet (and I know I should - just trying to build up my knowledge bit by bit). When executing the same script through terminal it runs fine. I uninstalled script and tried script-runner instead but that still produced the same error. I reinstalled script (and noticed that said ‘build failing’) and still this error.

stumped at the moment
thank you for your help though!


#4

open() exists in Python 2. I don’t see where that would be the problem. What it sounds like is that the Atom packages are trying to execute the command from a different place in the file tree than where your script actually resides. When you have the project open in Atom, what’s the folder structure like? If the script isn’t on the top level, try opening a new instance of Atom with the script’s directory as the project folder and then see if you can run it.


#5

that fixed it - will continue to run it from the scripts folder as the project (had the project set a few layers up)

thank you


#6

You might also be able to set up a script profile to meet your needs. Good luck.


#7

Hello, new to Atom and everything else too.

I have the same issue opening a file as described above, the scrip and data file are located in the project folder but it still doesn’t open.

I have a snippet that works fine in other IDE’s and IDLE as well, but not in Atom with script package. When I open a Windows Power shell using Atom platformio_ide_terminal, it runs fine also.

import csv

csvfile = open('batting1.csv', 'r', newline='')
csvreader = csv.DictReader(csvfile, delimiter = ',', quotechar = '"')
for line in csvreader:
    print(line)

Any insights would be appreciated, thanks.


#8

I can’t help you unless you tell me what is going wrong with script. If you are failing to run something, you should be getting an error message of some sort.


#9

Oops, you’re right, here is the error message:

Traceback (most recent call last):
File “D:\Users\Bill\My Documents\Education\Rice Univ\Introduction to Scripting in Python\Python Data Analysis\04.06 Project_ Analyzing Baseball Data\readfile_test.py”, line 3, in
csvfile = open(‘batting1.csv’, ‘r’, newline=’’)
FileNotFoundError: [Errno 2] No such file or directory: 'batting1.csv


#10

Okay, try this and tell me if it works:

csvfile = open('D:\Users\Bill\My Documents\Education\Rice Univ\Introduction to Scripting in Python\Python Data Analysis\04.06 Project_ Analyzing Baseball Data\batting1.csv', 'r', 'newline='')

#11

I tried your code as is and it gave an EOL error:

Traceback (most recent call last):
File “D:\Users\Bill\My Documents\Education\Rice Univ\Introduction to Scripting in Python\Python Data Analysis\04.06 Project_ Analyzing Baseball Data\readfile_test.py”, line 3
csvfile = open(‘D:\Users\Bill\My Documents\Education\Rice Univ\Introduction to Scripting in Python\Python Data Analysis\04.06 Project_ Analyzing Baseball Data\batting1.csv’, ‘r’, ‘newline=’’)
^
SyntaxError: EOL while scanning string literal

But I noticed your had an extra quote in front of the newline keyword argument. When I removed this quote, I got the following error:

Traceback (most recent call last):
File “D:\Users\Bill\My Documents\Education\Rice Univ\Introduction to Scripting in Python\Python Data Analysis\04.06 Project_ Analyzing Baseball Data\readfile_test.py”, line 3
csvfile = open(‘D:\Users\Bill\My Documents\Education\Rice Univ\Introduction to Scripting in Python\Python Data Analysis\04.06 Project_ Analyzing Baseball Data\batting1.csv’, ‘r’, newline=’’)
^
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape


#12

Yeah, my bad. I didn’t notice that.

You’re probably going to have to make the current working directory be read in inside your code because script doesn’t appear to provide enough information to python for python to find batting1.csv. You can use the os library to read the path to the script file and can extrapolate from there. Try this for an example:

import os
print os.getcwd()

#13

I added the import os line which works fine, i.e. print(os.getcwd()), but unfortunately it doesn’t fix the issue, I still get the identical unicode error.

BUT, I found the problem: the Windows ‘’" must be replace by “/” in the open() function otherwise the unicode error is triggered in python:

import csv

csvfile = open('D:/Users/Bill/My Documents/Education/Rice Univ/Introduction to Scripting in Python/Python Data Analysis/04.06 Project_ Analyzing Baseball Data/batting1.csv', 'r', newline='')
csvreader = csv.DictReader(csvfile, delimiter = ',', quotechar = '"')
for line in csvreader:
    print(line)

So, ultimately my original issue is resolved by a) adding the full path to filename, and b) using forward slash in the path. Import os is not needed.

I assumed that the Atom editor should just pass the script as is to python to execute, assuming the editor is agnostic to the language, but I guess it is not that simple. If this can be fixed in the Atom code so it behaves without the full path, this would resolve the problem in numerous cases for me. I’m not sure how to enter a bug report if this is indeed a real bug.
Thanks for your help.


#14

Oh, duh. I feel bad now for not paying attention and noticing the glaringly obvious answer. Yes, \ is problematic because \ is also how you indicate special characters (\n is a newline, \t is a tab, etc.)

I assumed that the Atom editor should just pass the script as is to python to execute, assuming the editor is agnostic to the language, but I guess it is not that simple. If this can be fixed in the Atom code so it behaves without the full path, this would resolve the problem in numerous cases for me. I’m not sure how to enter a bug report if this is indeed a real bug.

The incorrect assumption is that Atom has anything to do with this. You are using a package to run code and that package has absolute control over the manner in which that code is run. Yes, it will pass the code through Atom’s BufferedProcess API, but that’s just a wrapper on Node’s child_process module that does some behind-the-scenes stuff regarding how processes should be organized. The editor itself has zero input on the procedure.