Python IOError: [Errno 2] from recursive directory call - python

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

Related

issue with counting lines of python file

I am traversing a directory, and if python file is found, trying to find the number of lines of code in it.
I am not really sure why I face the below issue.
for dirpath, dirnames, filenames in os.walk(APP_FOLDER):
for file in filenames:
if pathlib.Path(file).suffix == ".py":
num_lines = len(open(file).readlines()) #causes issue
print(
f"Root:{dirpath}\n"
f"file:{file}\n\n"
f"lines: {num_lines}" //
)
Érror:
Traceback (most recent call last):
File "C:\Users\XXXX\PycharmProjects\pythonProject1\main.py", line 11, in <module>
num_lines = len(open(file).readlines())
FileNotFoundError: [Errno 2] No such file or directory:
If i remove that line, the code works as expected. There is a single line code within. Any guidance here?
file is the file's name, not its path, to get its full path, join the name with the directory path dirpath using os.path.join. Also, it is better to use a with statement when dealing with files since it automatically closes the file:
file_path = os.path.join(dirpath, file)
with open(file_path) as f:
num_lines = len(f.readlines())
You can also try this way. You have to get the os.path.abspath(file) path of the file then only it'll open.
and always with contect_managers for IO operation.
for dirpath, dirnames, filenames in os.walk(APP_FOLDER):
for file in filenames:
if pathlib.Path(file).suffix == ".py":
try:
with open(os.path.abspath(file)) as f:
print(len(f.readlines()))
print(os.path.abspath(file))
except:
pass

Decompressing .bz2 files in a directory in python

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

Python suddenly cannot find files only when I want to rename or copy them

I'm trying to very simply copy a load of files from one directory to another and then rename the files (to replace whitespaces and %20 with _). In my root directory, I have:
optimiser.py (what I use to run the script)
Folder (named 'Files to Improve' containing basic text files)
Folder (named 'Finished' where I want every file to be copied to (empty))
It seems that the files can be found because this code successfully prints every file (and their respective new filenames):
import os
from shutil import copyfile
files_to_improve_dir = 'Files to Improve'
finished_files_dir = 'Finished'
for f in os.listdir(files_to_improve_dir):
new_filename = f.replace(' ','_').replace('%20','_').lower()
print('f:', f, '--- n:', new_filename)
The first problem occurs when I add copyfile(f, finished_files_dir) to the bottom, returning this error:
f: FILE.txt --- n: file.txt
Traceback (most recent call last):
File "optimiser.py", line 10, in <module>
copyfile(f, finished_files_dir)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 259, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'FILE.txt'
Does anyone know what this error is and why it occurs?
Secondly, if I instead add this to the bottom instead of the copy file line:
if (f != new_filename):
os.rename(f, new_filename)
It returns this error instead:
f: FILE.txt --- n: file.txt
Traceback (most recent call last):
File "optimiser.py", line 12, in <module>
os.rename(f, new_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'FILE.txt' -> 'file.txt'
I don't know why it suddenly now cannot find the files, despite it printing them in the previous code. If it couldn't find the files, how could it have printed them?
Thanks for any help here. The complete code is below:
import os
from shutil import copyfile
files_to_improve_dir = 'Files to Improve'
finished_files_dir = 'Finished'
for f in os.listdir(files_to_improve_dir):
new_filename = f.replace(' ','_').replace('%20','_').lower()
print('f:', f, '--- n:', new_filename)
copyfile(f, finished_files_dir) #error 1
if (f != new_filename): #error 2
os.rename(f, new_filename) #error 2
Change your
copyfile(f, finished_files_dir) #error 1
to
copyfile(os.path.join(files_to_improve_dir, f),
os.path.join(finished_files_dir, f))
and
os.rename(f, new_filename) #error 2
to
os.rename(os.path.join(files_to_improve_dir, f),
os.path.join(files_to_improve_dir, new_filename))
One more hint: When renaming your file, there might araise duplicates (e.g. bla bla and bla%20bla will both be renamed to bla_bla), so you may want to check if the destination exists already...

File exists but open says it doesnt

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.

Creating text file: IOError: [Errno 21] Is a directory: '/Users/muitprogram/PycharmProjects/untitled/HR/train.txt'

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.

Categories