#自定义函数:
Import OS
Path= "D:\\temp_del\\a"
def GCI (path):
"" "This is a statement" ""
Parents = Os.listdir (path)
For the parent in parents:
Child = Os.path.join (path,parent)
#print (Child)
If Os.path.isdir (child):
GCI (Child)
# Print (Child)
Else
Print (child)
GCI (PATH)
Print (gci.__doc__) #显示函数声明部分内容
def function_name (param):
"" "" "This is a statement." "" "" ""
You can use the function's property __doc__ to return the declaration, such as print (FUNCTION_NAME.__DOC__)
#使用os. Walk method Traversal:
Import OS
Path= "D:\\temp_del\\a"
For I in Os.walk (path):
Print (i)
return Result:
(' d:\\temp_del\\a ', [' AFA ', ' x64 '], [' Audiofilteragent.ini ', ' Setup.exe '])
(' D:\\temp_del\\a\\afa ', [' 222 '], [' CAudioFilterAgent.exe ', ' CAudioFilterAgent64.exe '])
(' d:\\temp_del\\a\\afa\\222 ', [], [' New journal document-copy. Jnt ', ' new journal document. Jnt '])
(' d:\\temp_del\\a\\x64 ', [' BBB '], [' Audiofilteragent.ini ', ' Setup64.exe '])
(' d:\\temp_del\\a\\x64\\bbb ', [], [' CAudioFilterAgent.exe ', ' CAudioFilterAgent64.exe '])
Return result Description:
The return is a ternary tupple (Dirpath, Dirnames, filenames),
The first one is the starting path, the second is the folder under the starting path, and the third is the file under the starting path.
Dirpath is a string that represents the path to the directory,
Dirnames is a list that contains the names of all subdirectories under Dirpath,
Filenames is a list that contains the name of a non-directory file. These names do not contain path information, and you need to use Os.path.join (Dirpath, name) if you need to get the full path.
=====================
Method One: Os.listdir
#!/usr/bin/python#-*-coding:utf-8-*-import osdef GCI (filepath): #遍历filepath下所有文件, including subdirectories files = Os.listdir ( filepath) for fi in Files: fi_d = Os.path.join (filepath,fi) if Os.path.isdir (fi_d): GCI (fi_d) else: print os.path.join (filepath,fi_d) #递归遍历 all files in the/root directory GCI ('/root ')
Method Two: Os.walk
#!/usr/bin/python#-*-coding:utf-8-*-import osfor fpathe,dirs,fs in Os.walk ('/root '): For F in fs: print Os.pat H.join (FPATHE,F)
List all Files:
[I for I in Os.listdir ('. ') if Os.path.isdir (i)]
List all. py files [i for I in Os.listdir ('. ') if Os.path.isfile (i) and Os.path.splitext (i) [1]== '. Py ']
Reproduced Python recursively iterates through all the files in the directory