before I read someone on the Internet saying “os.path.isdir() determines that absolute path must be written”, I thought Python has an iteration context, why not?Therefore,
is verified in this paper
code section
Consider using a path
variable to refer to the current traversal element’s absolute path (correct practice)
def search(root, target):
items = os.listdir(root)
for item in items:
path = os.path.join(root, item)
if os.path.isdir(path):
print('[-]', path)
search(path, target)
elif path.split('/')[-1] == target:
print('[+]', path)
else:
print('[!]', path)
normal traversal result
if I write it this way, I’m going to replace all the paths with item
(iterative element)
def search(root, target):
items = os.listdir(root)
for item in items:
if os.path.isdir(item):
print('[-]', item)
search(os.path.join(root, item), target)
elif item == target:
print('[+]', os.path.join(root,item))
else:
print('[!]', os.path.join(root, item))
can be seen that this does not work: if you use the item
to iterate over the current path, you will recursively recursively refer to the current path only two levels further down (subfolders, subfolder files), you can see that the missing context management mechanism is completely ineffective here
reflection and summary
1) how would you react if you just changed the iteration mechanism of the passing element for each recursion based on the correct traversal writing?
In the above example, just change search(os.path.join(root, item), target)
to search(item, target)
:
this is easy to see because the first time you call search(..)
is passed in an absolute path. If the relative path is passed in because there is no context, it cannot correctly locate the location of the file
2)os.path. Abspath (path)
method can replace os.path. Join (root, current)
?
(fog) original os.path. Abspath (..)
means: relative path to the current working directory! (not the relative path to the system root!)
so, os.path. Abspath (..) code> and
OS path. Join (..)
is two completely different things that cannot be replaced by
3) what exactly is a Python file?
again, change each output to use type(..)
function package form
let's look at another example
In line with the Python "everything is an object" belief, the path we enter (including its iteration version, and the variable used to accept it) is a string STR
object, and the file handle (pointer) used to describe the file is a
object
In general, we search and filter folders and files horizontally, using the path description of STR
type. However, to read or write a specific file (not a folder), we need to use the file handle of _io.TextIOWrapper
type. Unlike Java, which is completely encapsulated as a File(String path)
object, it reduces the role of the File path as a single individual -- but Python takes the path as a separate one, and many of our File operations (broadly defined as finding a specific File, not just a File IO) are based on the File path as
4) thinking: is there any way to traverse the file other than the method given at the beginning?
answer: yes
Chdir (path) os.chdir(path) path
but we do not recommend this method because it modifies the global variable, which is validated before and after the function call using the os.getcwd()
function
First switch the working directory to the root directory, then execute the custom change()
function
as you can see, by this time the global current working directory
has changed
Read More:
- Python traverses all files under the specified path and retrieves them according to the time interval
- The Python DOM method iterates over all the XML in a folder
- Python automatically generates the requirements file for the current project
- Change the Python installation path in Pycharm
- Python: How to Reshape the data in Pandas DataFrame
- You can run the Ansible Playbook in Python by hand
- Python classes that connect to the database
- Python+ Pandas + Evaluation of Music Equipment over the years (Notes)
- Python: How to Delete Empty Files or Folders in the Directory
- Python environment error, bad interpreter: there is no file or directory
- Python FileNotFoundError: [Errno 2] No such file or directory: ‘objects/epsilon.pkl
- The solution of no such file or directory and cannot load native module running error of python3 pyinstaller after packaging
- [Solved] Python project runs the open() function error: FileNotFoundError: [Errno 2] No such file or directory
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- [Solved] Windows Python Upzip rar File Error: Cannot find working tool
- [Solved] ValueError: Connection error, and we cannot find the requested files in the cached path…
- Python PIP Fatal error in launcher: Unable to create process using ‘“e:\program files\programdata
- Python errors: valueerror: if using all scalar values, you must pass an index (four solutions)
- Importerror: DLL load failed: unable to find the specified module in Python
- Python TypeError: not all arguments converted during string formatting [Solved]