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
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
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 use the following code to unzip all the zip folders in my root folder; this code was found on this thread:
Unzip zip files in folders and subfolders with python
rootPath = u"//rootdir/myfolder" # CHOOSE ROOT FOLDER HERE
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
print(os.path.join(root, filename))
zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
but I keep getting this error that says FileNotFoundError saying the xlsx file does not exist:
Traceback (most recent call last):
File "//rootdir/myfolder/Python code/unzip_helper.py", line 29, in <module>
zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
File "//rootdir/myfolder/Python\Python36-32\lib\zipfile.py", line 1491, in extractall
self.extract(zipinfo, path, pwd)
File "//myaccount/Local\Programs\Python\Python36-32\lib\zipfile.py", line 1479, in extract
return self._extract_member(member, path, pwd)
File "//myaccount/Local\Programs\Python\Python36-32\lib\zipfile.py", line 1542, in _extract_member
open(targetpath, "wb") as target:
FileNotFoundError: [Errno 2] No such file or directory: '\\rootdir\myfolder\._SGS Naked 3 01 WS Kappa Coated and a very long very long file name could this be a problem i dont think so.xlsx'
My question is, why would it want to unzip this excel file anyways?!
And how can I get rid of the error?
I've also tried using r instead of u for rootPath:
rootPath = r"//rootdir/myfolder"
and I get the same error.
Any help is truly appreciated!
Some filenames and directory names may have extra dots in their names, as a consequence the last line, unlike Windows filenames can have dots on Unix:
zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
this line fails. To see how that happens:
>>> filename = "my.arch.zip"
>>> root = "/my/path/to/mydir/"
>>> os.path.join(root, os.path.splitext(filename)[0])
'/my/path/to/mydir/my.arch'
With or without extra dots, problems will still take place in your code:
>>> os.path.join(root, os.path.splitext(filename)[0])
'/my/path.to/mydir/arch'
If no '/my/path.to/mydir/arch' can be found, FileNotFoundError will be raised. I suggest that you be explicit in you path, otherwise you have to ensure the existence of those directories.
ZipFile.extractall(path=None, members=None, pwd=None)
Extract all members from the archive to the current working directory. path specifies a different directory to extract to...
Unless path is an existent directory, FileNotFoundError will be raised.
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