General idea:
Os.path.abspath ('. ') #获取当前路径
Os.listdir #获取指定的文件夹包含的文件或文件夹的名字的列表
Os.path.join #用获取的文件名拼接路径
Os.path.isdir #判断路径是否为目录, for recursion
1 #-*-coding:utf-8-*-2 3 __author__='Dou-wei'4 5 ImportOS6 ImportSYS7 8file_name = sys.argv[1]#get the user's terminal input to name the file9Start_path = Os.path.abspath ('.')#Get current pathTenHost = Os.path.join (Start_path,file_name)#Stitching Path Onef = open (host,'W')#Create a writable file in a stitched path A - defSearch (path): - forXinchOs.listdir (PATH):#os.listdir (path) Returns a list of the names of the files or folders that are contained in the specified folder theCurrent_name = Os.path.join (path,x)#Stitching Path -F.write (Current_name)#write to the path that contains the file name -F.write ('\ n')#line Break - ifOs.path.isdir (Current_name):#determine if the path is a directory +Search (Current_name)#recursive execution, further exploration - + defMain (): A Try: at Search (Start_path) - exceptException,e:#Output error message - Printe - - if __name__=='__main__': -Main ()
View Code
Approximate effect:
Python Novice Grey Hat script Practice---file path traversal collection