Path.rglob() raising FileNotFoundError on Linux system - python

I am creating a dependency diagram of all python modules on my Linux machine. I want the paths to be displayed so that I can correctly identify where in the system an import is.
I am using .rglob() to search through the machine starting at the root directory. The only problem with this is that I get a FileNotFoundError when the program gets to the /proc/ directory.
If there is a better way to search for the path of imported modules or if there is a way to avoid the /proc/ directory entirely please let me know below.
Here is my code:
def find_import_path(import_name):
"Get the path of an import."
big_globber = Path('/').rglob(f'{import_name}')
matches = [path for path in big_globber]
# One match
if len(matches) == 1:
return str(matches[0])
# No matches..Should not be possible - scripts on this machine are importing the import_name.
elif not matches:
return ''
# Multiple matches.
else:
return ''.join(f'{match}\n' for match in matches)
The error will occur on line 4.
I tried some exception handling already and had the program redundantly call back to find_import_path() which worked but ended up having an OSError pop from somewhere deeper within /proc/ again.
Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/bushbak2/projects/software-dependency-diagram/depend.py", line 216, in <module>
main()
File "/home/bushbak2/projects/software-dependency-diagram/depend.py", line 20, in main
generate_graphviz_diagram(parse_script(directory_of_execs))
File "/home/bushbak2/projects/software-dependency-diagram/depend.py", line 129, in generate_graphviz_diagram
imp_path = find_import_path(import_module)
File "/home/bushbak2/projects/software-dependency-diagram/depend.py", line 202, in find_import_path
matches = [path for path in big_globber]
File "/home/bushbak2/projects/software-dependency-diagram/depend.py", line 202, in <listcomp>
matches = [path for path in big_globber]
File "/usr/local/lib/python3.9/pathlib.py", line 1179, in rglob
for p in selector.select_from(self):
File "/usr/local/lib/python3.9/pathlib.py", line 599, in _select_from
for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
File "/usr/local/lib/python3.9/pathlib.py", line 589, in _iterate_directories
for p in self._iterate_directories(path, is_dir, scandir):
File "/usr/local/lib/python3.9/pathlib.py", line 589, in _iterate_directories
for p in self._iterate_directories(path, is_dir, scandir):
File "/usr/local/lib/python3.9/pathlib.py", line 578, in _iterate_directories
with scandir(parent_path) as scandir_it:
FileNotFoundError: [Errno 2] No such file or directory: '/proc/2812016'

Related

Why can't i create a folder when he doesn't exist?

i 'm trying to make a for loop who browse files in a specific directory while creating a folder if he doesn't exist with this solution. here is the code:
import ftputil
host=ftputil.FTPHost('x.x.x.x',"x","x") #connecting to the ftp server
mypathexist='./CameraOld' (he is here: /opt/Camera/CameraOld
mypath = '.' #it put you in /opt/Camera (it's the default path configured)
host.chdir(mypath)
files = host.listdir(host.curdir)
for f in files: #i browse the files in my folders
if f==mypathexist: #if a file is named CameraOld (it's a folder)
isExist=True
break
else: isExist=False #if 0 file are named like it
print(isExist)
if isExist==False: #if the file doesn't exist
host.mkdir(mypathexist) #create the folder
else:
print("ok")
The problem is that isExist is always false so the script try to create a folder who is already created. And i don't understand why.
Here's the output:
False #it's the print(isExist)
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 695, in command
self._session.mkd(path)
File "/usr/lib/python3.10/ftplib.py", line 637, in mkd
resp = self.voidcmd('MKD ' + dirname)
File "/usr/lib/python3.10/ftplib.py", line 286, in voidcmd
return self.voidresp()
File "/usr/lib/python3.10/ftplib.py", line 259, in voidresp
resp = self.getresp()
File "/usr/lib/python3.10/ftplib.py", line 254, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 CameraOld: file exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/user/Bureau/try.py", line 16, in <module>
host.mkdir(mypathexist)
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 697, in mkdir
self._robust_ftp_command(command, path)
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 656, in _robust_ftp_command
return command(self, tail)
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 694, in command
with ftputil.error.ftplib_error_to_ftp_os_error:
File "/usr/local/lib/python3.10/dist-packages/ftputil/error.py", line 195, in __exit__
raise PermanentError(
ftputil.error.PermanentError: 550 CameraOld: file exist
Debugging info: ftputil 5.0.4, Python 3.10.4 (linux)
I would bet your mypathexist is not correct. Or the other way around, your file list, doesn't hold the strings in that condition you assume it does.
Take a look at your condition by hand. Print out f in your loop. Is it what you would expect to be?
In the end, Python is simply comparing Strings.

Why do I have error when I change instapy like_until file?

I had some problems with liking
Cannot detect post media type
I read that I should change one line like_until (span to div) but when I changed that, I got these error:
Traceback (most recent call last): File "c:\Users\lenovo\Desktop\SubDesk\Click Bloom\click_bloom_v1.0.0\main.py", line 72, in <module> session.like_by_tags(['bitcoin'], amount=11) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\instapy\instapy.py", line 1957, in like_by_tags inappropriate, user_name, is_video, reason, scope = check_link( File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\instapy\like_util.py", line 726, in check_link quash = re.search(dont_likes_regex, image_text, re.IGNORECASE) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\re.py", line 201, in search return _compile(pattern, flags).search(string) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\re.py", line 304, in _compile p = sre_compile.compile(pattern, flags) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_compile.py", line 764, in compile p = sre_parse.parse(p, flags) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_parse.py", line 948, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_parse.py", line 443, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_parse.py", line 671, in _parse raise source.error("multiple repeat", re.error: multiple repeat at position 8
what shoud I do?
Installing instapy from the command line has this error.
To solve that:
Delete any instapy folder in this location:
C:\Users[computer-user]\AppData\Local\Programs\Python\Python39\Lib\site-packages
Go to https://github.com/timgrossmann/InstaPy
Download instaPy-master.zip
Extract the downloaded file and copy the instapy folder from
\InstaPy-master
to
C:\Users[computer-user]\AppData\Local\Programs\Python\Python39\Lib\site-packages
Then run your code
problem solved!
one of my tag contain + and it makes problem

Python3.9.0 Idle Wont Open How Do I Fix This?

I am not sure why my python 3.9 idle isnt opening VIDEO I have tried multiple times but it still isnt opening for me
I tried repairing it in control panel but it still isnt working
I also tried to reinstall it but I am still having the same problem it just wont open no matter how much I click
my idle path on my folder enter image description here
C:\Users\Habib\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.9
on my IDle properties it says V
TARGET: C:\Users\Habib\AppData\Local\Programs\Python\Python39\pythonw.exe "C:\Users\Habib\AppData\Local\Programs\Python\Python39\Lib\idlelib\idle.pyw"
START: C:\Users\Habib\AppData\Local\Programs\Python\Python39\
OUTPUT I GET WHEN I PUT py -m idlelib
Traceback (most recent call last):
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\__main__.py", line 6, in <module>
import idlelib.pyshell
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\pyshell.py", line 49, in <module>
from idlelib.colorizer import ColorDelegator
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\colorizer.py", line 6, in <module>
from idlelib.config import idleConf
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\config.py", line 766, in <module>
idleConf = IdleConf()
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\config.py", line 166, in __init__
self.LoadCfgFiles()
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\config.py", line 758, in LoadCfgFiles
self.userCfg[key].Load() #same keys
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\idlelib\config.py", line 77, in Load
self.read(self.file)
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\configparser.py", line 697, in read
self._read(fp, filename)
File "C:\Users\Habib\AppData\Local\Programs\Python\Python39\lib\configparser.py", line 1082, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.
file: 'C:\\Users\\Habib\\.idlerc\\config-main.cfg', line: 1
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Python 3.9 is very new and it may not be working with your system try switching back to python 3.8.6 and it looks like you havent added python to path
I had the same problem and I was trying to open (pythonw.exe)
it turns out that I had to open Python\Lib\idlelib\idle.py type(python file(no console))

Pychecker index error on first run

I've just installed pychecker on windows 7 Pro using "python setup.py install". When I run it on my script using the command:
c:\Python26\Scripts\pychecker -#100 finaltest17.py
I get the following error/traceback:
C:\Users\....\ToBeReleased>C:\Python26\python.exe C:\Python26\Lib\site-packages\pychecker\checker.py -#100 finaltest17.py
Processing module finaltest17 (finaltest17.py)...
Caught exception importing module finaltest17:
File "C:\Python26\Lib\site-packages\pychecker\pcmodules.py", line 533, in setupMainCode()
self.moduleName, self.moduleDir)
File "C:\Python26\Lib\site-packages\pychecker\pychecker\utils.py", line 184, in findModule()
handle, filename, smt = _q_find_module(p, path)
File "C:\Python26\Lib\site-packages\pychecker\pychecker\utils.py", line 162, in _q_find_module()
if not cfg().quixote:
File "C:\Python26\Lib\site-packages\pychecker\pychecker\utils.py", line 39, in cfg()
return _cfg[-1]
IndexError: list index out of range
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\pychecker\checker.py", line 364, in <module>
sys.exit(main(sys.argv))
File "C:\Python26\Lib\site-packages\pychecker\checker.py", line 337, in main
importWarnings = processFiles(files, _cfg, _print_processing)
File "C:\Python26\Lib\site-packages\pychecker\checker.py", line 270, in processFiles
loaded = pcmodule.load()
File "C:\Python26\Lib\site-packages\pychecker\pcmodules.py", line 477, in load
return utils.cfg().ignoreImportErrors
File "C:\Python26\Lib\site-packages\pychecker\pychecker\utils.py", line 39, in cfg
return _cfg[-1]
IndexError: list index out of range
If anyone could point me in the right direction that would be great.
Thanks
Stewart
Problem resolved.
I found the following support request on SourceForge which refers to a need to use short format (8.3) path and filenames in pychecker.bat and not long format as is allowed in newer versions of Windows.
https://sourceforge.net/p/pychecker/support-requests/7/#96cb

Python : Copying directory tree with symbolic links in it

In Python I am trying to copy a directory (actually, its the Jenkins jobs directory), but it contains symbolic links in it, when I copy I get:
Traceback (most recent call last):
File "BackupJenkinsJobs.py", line 272, in <module>
main()
File "BackupJenkinsJobs.py", line 208, in main
distutils.dir_util.copy_tree(JenkinsJobSrc, cleanJobsDir, preserve_symlinks=False)
File "c:\Python27\lib\distutils\dir_util.py", line 163, in copy_tree
verbose=verbose, dry_run=dry_run))
File "c:\Python27\lib\distutils\dir_util.py", line 163, in copy_tree
verbose=verbose, dry_run=dry_run))
File "c:\Python27\lib\distutils\dir_util.py", line 167, in copy_tree
dry_run=dry_run)
File "c:\Python27\lib\distutils\file_util.py", line 148, in copy_file
_copy_file_contents(src, dst)
File "c:\Python27\lib\distutils\file_util.py", line 32, in _copy_file_contents
fsrc = open(src, 'rb')
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Program Files (x86)\\Jenkins\\jobs\\AutoRunTemplate\\builds\\lastFailedBuild'
I am using the following code:
try:
distutils.dir_util.copy_tree(JenkinsJobSrc, cleanJobsDir, preserve_symlinks=False)
except distutils.errors.DistutilsFileError as e:
print("Unable to copy Jenkins jobs. Error: %s".format(e))
return
Any assistance would be appreciated on how to copy, ignoring the links, as preserve_symlinks doesn't appear to work.

Categories

Resources