#
#!/usr/bin/python 读取目录下所有文件的文件名的写入到wenjianming.txt中
#python3运行环境,输出结果txt写入文件的所在目录为当前readfilename.py的python程序文件目录
import os
def ListFilesToTxt(dir,file,wildcard,recursion):
exts = wildcard.split(" ")
files = os.listdir(dir)
for name in files:
fullname=os.path.join(dir,name)
if(os.path.isdir(fullname) & recursion):
ListFilesToTxt(fullname,file,wildcard,recursion)
else:
for ext in exts:
if(name.endswith(ext)):
file.write(name + "\n") #换行符每写入一个文件名换行
break
def Test():
#文件路径
dir =r"K:\GUOHY\python\7days" #前面加入r作为文件路径转义符否则k:\\GUOHY\python\7days
outfile = "wenjianming.txt" #写入的txt文件名
wildcard = ".mp4 .txt .exe .dll .lib" #要读取的文件类型;
file = open(outfile,"w")
if not file:
print ("cannot open the file %s for writing" % outfile)
ListFilesToTxt(dir,file,wildcard, 1)
file.close()
Test()
The example works when you have a lot of documents that need to be written in a catalogue or chapter.
![]()
未经允许不得转载:萌萌Apprentissage » The python example is practical in that the filenames of all files under a folder are read and written to txt text
Python batches to extract mainstream search engine pictures
Python's word crawling across 100 degrees.
PythonTaiichi Taipei Artificial Intelligence Tensoflow Graphic Processing
Selenium with Tor Browser using Python
I'm gonna get you a new one, shell.cloud.google on the line.
run crontab with python selenium tor browser display in linux