I'm getting a permission denied error when trying to use glob to get a filename list and csv to open csv files for dictionary reading. Here is my code:
import csv
import glob
#Filepath variable for the CSV files
path = "C:\\Users\\josh\\Data"
for filename in glob.glob(path):
with open(filename) as csv_file:
for row in csv.DictReader(csv_file):
print row
I've tried running some file opening tests with the following code and it works perfectly. So I know I can open, read, write to this folder:
open('C:\\Users\\josh\\Data\\testing.txt', 'w').write('this is a test')
open('C:\\Users\\josh\\Data\\03142016.csv')
Lastly, here is the full traceback if that helps:
Traceback (most recent call last):
File "<ipython-input-10-49215e5eb704>", line 1, in <module>
runfile('C:/Users/josh/.spyder2/csvparser2.0.py', wdir='C:/Users/josh/.spyder2')
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "C:/Users/josh/.spyder2/csvparser2.0.py", line 41, in <module>
with open(filename) as csv_file:
IOError: [Errno 13] Permission denied: 'C:\\Users\\josh\\Data'
Any ideas? Thank you for your help.
Right now you're doing:
path = "C:\\Users\\josh\\Data"
for filename in glob.glob(path):
glob.glob() on only a pathname without any special globbing characters (*, ?, etc) will just return that pathname. In this case the directory name, which you're not allowed to open() since that doesn't make a lot of sense.
You probably intended to write:
glob.glob(path + '\\*'):
or:
glob.glob(path + '\\*.csv'):
Bonus tip
You already figured out that using open('C:\\Users\\josh\\Data\\03142016.csv') works fine, which is a good first step in solving the problem. You could have found out the error by using basic "printf-debugging":
path = "C:\\Users\\josh\\Data"
for filename in glob.glob(path):
print(filename)
with open(filename) as csv_file:
[.. trim ..]
This would have printed out C:\\Users\\josh\\Data, and not C:\\Users\\josh\\Data\\03142016.csv :-)
Lesson: when in doubt, print() out as many values as you can, and check if they're what you expect ;-)
Related
dir_path = os.path.dirname(os.path.realpath(__file__))
from os.path import isfile, join
onlyfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]
print(onlyfiles);
with open("config.json", 'r') as jsondata:
config = json.loads(jsondata.read())
Running this code, somehow, triggers a non existing error despite the file being listed during
print(onlyfiles);
Here is the full output log from the console.
Traceback (most recent call last):
['builder.py', 'builder.spec', 'builderloader2.rb', 'config.json',
'Run.bat', 'Run.bat.lnk', 'test.json']
File "C:/Users/cody.jones/Desktop/Builder Generator Release/builder.py",
line 26, in <module>
with open("config.json", 'r') as jsondata:
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
Process finished with exit code 1
provide full path to open() instead of just file name as by default it will look for file in same directory
try:
open(r"C:/Users/cody.jones/Desktop/Builder Generator Release/config.json", "r")
The script will look for config.json in the current working directory - which presumably is not the same as the folder that the script resides in.
Update your open call to include the path you've already generated.
with open(os.path.join(dir_path, "config.json"), 'r')) as jsondata:
By doing it this way (rather than just including the absolute path) this script will still work if you move it to a different directory or computer so long as you keep the script and config together.
I am trying to write to create and write to a text file. However, the error
Traceback (most recent call last):
File "/Users/muitprogram/PycharmProjects/untitled/histogramSet.py", line 207, in <module>
drinktrainfile = open(abs_file_path, "w")
IOError: [Errno 21] Is a directory: '/Users/muitprogram/PycharmProjects/untitled/HR/train.txt'
shows up. There are other instances of this in Stack Overflow, however, none of these deal with creating and writing a new file. The directories all exist however- only the file is being created The code that does this is:
import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] # i.e. /path/to/dir/
rel_path = str(j) + "/train.txt" # HR/train.txt
abs_file_path = os.path.join(script_dir, rel_path) #/path/to/dir/HR/train.txt
drinktrainfile = open(abs_file_path, "w")
EDIT: train.txt shows up, except as a directory. How do I make it a text file?
The resource is actually a directory. It was very likely a mistake, as it is not likely that somebody would have created a directory by that name. First, remove that directory, and then try to create and open the file.
You can open the file with open('/Users/muitprogram/PycharmProjects/untitled/HR/train.txt', 'w'), assuming that the file does not already exist.
I'm trying to access a .txt file in Python and I can't figure out how to open the file. I ended up copying the contents into a list directly but I would like to know how to open a file for the future.
If I run this nothing prints. I think it's because Python is looking in the wrong folder/directory but I don't know how to change file paths.
sourcefile = open("CompletedDirectory.txt").read()
print(sourcefile)
The file CompletedDirectory.txt is probably empty.
If Python could not find the file, you would get a FileNotFoundError exception:
>>> sourcefile = open("CompletedDirectory.txt").read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'CompletedDirectory.txt'
Note that using read() in this way is not recommended. You're not closing the file properly. Use a context manager:
with open("CompletedDirectory.txt") as infile:
sourcefile = infile.read()
This will automatically close infile on leaving the with block.
You can get the current working directory:
import os
os.getcwd()
Then just concat it with the file container directory
os.path.join("targetDir", "fileName")
Bascially I am reading in a file called Sounding, which has its name as '12142014_2345.csv' and I want to save it as
'12142014_2345_Averaged.csv'
Below is the code I have leading up to it.
basename = os.path.basename(Sounding)
basename,ext = os.path.split(basename)
with open(os.path.join(basename+'_Averaged'+ext)) as f:
w = csv.DictWriter(f, rows_1[0].keys())
w.writeheader()
And this is my error.
Traceback (most recent call last):
File "C:\Users\Bud\Desktop\OWLES RECENT\Moving Average.py", line 151, in <module>
with open(os.path.join(basename+'_Averaged'+ext)) as f:
IOError: [Errno 2] No such file or directory: u'_AveragedUIllinois_20131207Singular.csv'
I'm not exactly sure what I am doing wrong with it.
You want to use os.path.splitext (to split the extension) instead of split (that splits last path element).
And don't forget to open the file in write mode (and check your indentation):
basename,ext = os.path.splitext(basename)
with open(os.path.join(basename+'_Averaged'+ext), 'w') as f:
w = csv.DictWriter(f, rows_1[0].keys())
w.writeheader()
Looking at the file name in the trace, you can see '_Averaged' coming before the full file name. os.path.split looks for the directory divider (usually slash). I think you want os.path.splitext, which splits the string into path and extension. Since you already have just the basename, the path will be the filename without the extension.
The code below is part of a program I am writing that runs a method on every .py, .sh. or .pl file in a directory and its folders.
for root, subs, files in os.walk("."):
for a in files:
if a.endswith('.py') or a.endswith('.sh') or a.endswith('.pl'):
scriptFile = open(a, 'r')
writer(writeFile, scriptFile)
scriptFile.close()
else:
continue
When writing the program, it worked in the directory tree I wrote it in, but when I moved it to another folder to try it there I get this error message:
Traceback (most recent call last):
File "versionTEST.py", line 75, in <module>
scriptFile = open(a, 'r')
IOError: [Errno 2] No such file or directory: 'enabledLogSources.sh'
I know something weird is going on because the file is most definitely there...
You'll need to prepend the root directory to your filename
scriptFile = open(root + '/' + a, 'r')
files contains only the file names, not the entire path. The path to the file can be obtained by joining the file name and the root:
scriptFile = open(os.path.join(root, a), "r")
You might want to have a look at
https://docs.python.org/2/library/os.html#os.walk