参考一下我的这个代码:
参考一下:
import os
def searchfile(start_dir,extlist):
video_list=[]
for root,dir,files in os.walk(start_dir):
video_list.extend(os.path.join(root,file+'\n') for file in files if file.endswith(extlist))
return video_list
if __name__=='__main__':
start_dir=input('请输入要查找的路径:')
extlist=('.mp4','.avi','.rmvb') #这里用tuple才可以,不要用list,因为endswith的参数要tuple
program_dir=os.getcwd()
with open(program_dir+os.sep+'videolist.txt','w') as f:
f.writelines(searchfile(start_dir,extlist))
喜欢吃菠菜 发表于 2018-11-11 18:05
可以试试这样:
for root,dirs,files in os.walk('...'): #os.walk返回的结果是一个三元的tuple, root是当 ...
老哥 按照你的方法,我将代码修改为了:
os.chdir(start_path)
all_files = os.walk(os.getcwd())
for i in all_files:
if os.path.split(i[0])[1] != "venv": #os.path.split(i[0])[1]为root去掉路径的目录名
for each_file in i[2]:
code_type = os.path.splitext(each_file)[1]
if code_type not in code_list:
continue
code_type_dict.setdefault(code_type, 0)
code_type_dict[code_type] += 1
line_count.setdefault(code_type, 0)
for eachline in each_file:
line_count[code_type] += 1
你的代码太多用一些数字的东西,还有命名不是很清晰,所以理解起来有点混乱。
建议这样改:
for root,dirs,files in walk(os.getcwd()):
if os.path.split(root)[-1]!='venv':
for file in files:
if file.endswith(tuple(code_list)):#最好起一个贴切的变量名,比如extlists等
... #剩下的就是你处理你的字典的代码了