#coding =utf-8
Import OS
def find_file (arg,dirname,files):
#for I in ARG:
#print I
For file in Files:
File_path=os.path.join (Dirname,file)
print ' File_path: ', File_path
If Os.path.isfile (File_path) and (arg[0] in file or arg[1] in file):
print ' file:%s\n '%file_path
Os.path.walk (R ' D:\\test2 ', Find_file, ('. txt ', '. png '))
C:\python27\scripts>python task_test.py
File_path:d:\\test2\file1
File_path:d:\\test2\file1.txt
File:d:\\test2\file1.txt
File_path:d:\\test2\file2
File_path:d:\\test2\file1\file2.txt
File:d:\\test2\file1\file2.txt
File_path:d:\\test2\file2\file3
File_path:d:\\test2\file2\file3.txt
File:d:\\test2\file2\file3.txt
File_path:d:\\test2\file2\file3\file4.txt
File:d:\\test2\file2\file3\file4.txt
Look further at each cycle of Dirname,file:
#coding =utf-8
Import OS
def find_file (arg,dirname,files):
#for I in ARG:
#print I
For file in Files:
Print "DirName:%s, file:%s"% (dirname,file)
File_path=os.path.join (Dirname,file)
print ' File_path: ', File_path
If Os.path.isfile (File_path) and (arg[0] in file or arg[1] in file):
print ' file:%s\n '%file_path
Os.path.walk (R ' D:\\test2 ', Find_file, ('. txt ', '. png '))
C:\python27\scripts>python task_test.py
Dirname:d:\\test2, File:file1
File_path:d:\\test2\file1
Dirname:d:\\test2, File:file1.txt
File_path:d:\\test2\file1.txt
File:d:\\test2\file1.txt
Dirname:d:\\test2, File:file2
File_path:d:\\test2\file2
Dirname:d:\\test2\file1, File:file2.txt
File_path:d:\\test2\file1\file2.txt
File:d:\\test2\file1\file2.txt
Dirname:d:\\test2\file2, File:file3
File_path:d:\\test2\file2\file3
Dirname:d:\\test2\file2, File:file3.txt
File_path:d:\\test2\file2\file3.txt
File:d:\\test2\file2\file3.txt
Dirname:d:\\test2\file2\file3, File:file4.txt
File_path:d:\\test2\file2\file3\file4.txt
File:d:\\test2\file2\file3\file4.txt
Function Explanation:
Os.path.walk (Top, Func, Arg)
callback function (hook), which automatically invokes the specified function when an event occurs
Parameter description:
Top: Indicates the path of the directory tree that needs to be traversed
Func: A function that represents a callback function that processes a traversal path. The so-called callback function is used as a parameter of a function, and when triggered at a certain time, the program invokes a defined callback function to handle a task. The callback function must provide 3 parameters: the 1th parameter is walk () arg, the 2nd parameter represents the directory list dirname, and the 3rd parameter represents the file list names.
ARG: is a tuple passed to the callback function func, providing processing parameters for the callback function, the first parameter of the callback function is to receive the incoming tuple, the parameter arg can be empty)
#coding =utf-8
Import OS
#回调函数
#coding =utf-8
Import OS
#回调函数
#调find_file函数时 passed to Arg,dirname and files is the walk function.
def find_file (ARG, dirname, files):
#for I in ARG:
# Print I
For file in Files:
File_path = Os.path.join (dirname, file)
If Os.path.isfile (File_path):
Print "file:%s"%file_path
#调用
Os.path.walk (R "D:\test2", Find_file, ())
Python file operation, Os.path.walk () The callback function prints the file name