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