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.
Related
I would like to decompress a bunch of .bz2 files contained in a folder (where there are also .zst files). What I am doing is the following:
destination_folder = "/destination_folder_path/"
compressed_files_path="/compressedfiles_folder_path/"
dirListing = os.listdir(compressed_files_path)
for file in dirListing:
if ".bz2" in file:
unpackedfile = bz2.BZ2File(file)
data = unpackedfile.read()
open(destination_folder, 'wb').write(data)
But I keep on getting the following error message:
Traceback (most recent call last):
File "mycode.py", line 34, in <module>
unpackedfile = bz2.BZ2File(file)
File ".../miniconda3/lib/python3.9/bz2.py", line 85, in __init__
self._fp = _builtin_open(filename, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'filename.bz2'
Why do I receive this error?
You must be sure that all the file paths you are using exist.
It is better to use the full path to the file being opened.
import os
import bz2
# this path must exist
destination_folder = "/full_path_to/folder/"
compressed_files_path = "/full_path_to_other/folder/"
# get list with filenames (strings)
dirListing = os.listdir(compressed_files_path)
for file in dirListing:
# ^ this is only filename.ext
if ".bz2" in file:
# concatenation of directory path and filename.bz2
existing_file_path = os.path.join(compressed_files_path, file)
# read the file as you want
unpackedfile = bz2.BZ2File(existing_file_path)
data = unpackedfile.read()
new_file_path = os.path.join(destination_folder, file)
with bz2.open(new_file_path, 'wb') as f:
f.write(data)
You can also use the shutil module to copy or move files.
os.path.exists
os.path.join
shutil
bz2 examples
I'm running a python code (filename- images.py) that reads-
import gzip
f = gzip.open('i1.gz','r')
But it is showing the FileNotFoundError.
My folder containing images.py looks like-
New Folder/
images.py
i1.gz
(...Some other files...)
The problem is that you are not running the script from within the New Folder.
You can easily solve it by using the absolute path without hard-coding it:
from os import path
file_path = path.abspath(__file__) # full path of your script
dir_path = path.dirname(file_path) # full path of the directory of your script
zip_file_path = path.join(dir_path,'i1.gz') # absolute zip file path
# and now you can open it
f = gzip.open(zip_file_path,'r')
Check the current working directory of the script by doing:
import os
os.getcwd()
Then, compare this with your i1.gz absolute path. Then you should be able to see if there are any inconsistencies.
Are you run the script from the New Folder?
If you are in the folder, it should work:
c:\Data\Python\Projekty\Random\gzip_example>python load_gzip.py
but if you run the script from a parent folder with the folder name, it returned the error:
c:\Data\Python\Projekty\Random>python gzip_example\load_gzip.py
Traceback (most recent call last):
File "C:\Data\Python\Projekty\Random\gzip_example\load_gzip.py", line 2, in <module>
f = gzip.open('file.gz', 'r')
File "C:\Python\Python 3.8\lib\gzip.py", line 58, in open
binary_file = GzipFile(filename, gz_mode, compresslevel)
File "C:\Python\Python 3.8\lib\gzip.py", line 173, in __init__
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'file.gz'
The way I usually set working directory and work with files is as follow:
import os
pwd_path= os.path.dirname(os.path.abspath(__file__))
myfile = os.path.join(pwd_path, 'i1.gz')
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 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 ;-)
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