accessoriesleft.blogg.se

Python search for file
Python search for file









python search for file

Many more options are available, you can see these by running man find.įile should be able to guess the filetype even if the file has no extension etc (using the shebang line etc) - see here. To find the current directory of a file in Python, you can use the os.path.dirname() function from the os.path module. In this case, the pattern ‘. Also the use of -type f restrict find to look only for files - you could change this and then show symbolic links etc as well (some scripts are contained in /usr/lib etc and symlinked to /usr/bin/ etc). for file in files: print(file) The glob function returns a list of file paths that match the specified pattern. With find you could also add the -executable option to look for only executable files.

python search for file

Where / is your intended search directory. However, you can also use the file's mimetype (usually along the lines of text/x-python) to find it: find / -type f | while read in do if file -i "$" fi done This informs the interpreter of the script which program needs to be used to run it, and you could search for this to find python files from pathlib import Path for path in Path ('src').rglob ('.c'): print (path. #!/usr/bin/env python, #!/usr/bin/python2.7). 28 Answers Sorted by: 1753 Use from the pathlib module, which was introduced in Python 3.5. Most of these scripts will have a 'shebang' (or hashbang) line (e.g. py - Try running grep -rni python /usr/bin for example). Not all python files will have the file extension.











Python search for file