This time it was mainly the record about some of the modules or functions of another experiment.
os module
the os.listdir () function can display all the information in the specified directory. EG: the os.listdir (. " / " ) of course use Walk () OS (.stat; path) the os.path () to process the file path
os-file-dir file directory information
#coding=utf-8 import os for root,dirs,files in os.walk('./'); print root,dirs,files
The resulting file, the file name stored in the specified file inside
#coding=utf-8 import os for root,dirs,files in os.walk('./'): open('test.txt','a').write("{}{}{}".format(root,dirs,files))
#下面另外两种写法 #coding=utf-8 import os for root,dirs,files in os.walk('./'): export +="\n {}{}{}".format(root,dirs,files) open('demo01.py','w').write(export) #coding=utf-8 import os export = [] for root,dirs,files in os.walk('./'): export.append("\n {}{}{}".format(root,dirs,files)) open('demo01.py','w').write(''.join(export))
File search the desired effect
pysoso.py Python - E mysoso.txt # directory contents recorded as mysoso.txt Python pysoso.py -e sos / mysoso.txt # saved in the directory contents to mysoso.txt sos directory python pysoso.py -d sos - k China refueling # information in the search sos directory, look for the file or directory with Chinese refueling words.
Want to achieve this effect it is necessary to write the contents of the file is functional.
#coding=utf-8 import os def cdWalker(cdm,cdf): export = "" for root,dirs,file in os.walk(cdm): export += "\n{};{};{}".format(root,dirs,files) open(cdf,'w').write(export) cdWalker('./','cd1.txt') cdWalker('./','cd2.txt')
Join message
#coding=utf-8 import os,sys print sys.argv def cdWalker(cdm,cdf): export = "" for root,dirs,files in os.walk(cdm): export += "\n{};{};{}".format(root,dirs,files) open(cdf,'w').write(export) #cdWalker('./','mysoso.txt')
Join determine if ...... else,
#coding=utf-8 import os,sys CDM = './' def cdWalker(cdm,cdf): export = '' for root,dirs,files in os.walk(cdm): export += '\n{};{};{}'.format(root,dirs,files) open(cdf,'w').write(export) #cdWalker('./','mysos.txt') if "-e" ==sys.argv[1]: cdWalker(CDM,sys.argv[2]) print "Recording information {}" .Format (the sys.argv [2 ]) the else : Print '' ' PyC use: Python pyc.py -e mysos.txt # contents of the file is recorded as mysos.txt ' ''
--------------------------------------------- rest dividing line - -------------------------------------------------- -