0
点赞
收藏
分享

微信扫一扫

python文件目录练习题【一】

问题:找到在某个目录下面所有的文件内容里面有关键字"info"的文件,将这些文件路径存储在一个t1.pkl的文件里面

 

#coding=utf-8
import os;
import re;
import pickle;
#遍历改目录下所有文件的路径
root_path = "./txt";
file_list=[];
for dirpath,dirnames, fileNames in os.walk(root_path):
for fileName in fileNames:
file_list.append(os.path.join(dirpath,fileName));
#找到目标目录下所有的文件绝对路径
find_file_list=[];
flag=0;
for file in file_list:
content = open(file,"r").read();
if re.match(".*info.*",content):
flag=1;
if flag==1:
find_file_list.append(file);
#=====使用pick存储当前文件里面=====
disk_file="t1.pkl";
hd = open(disk_file,"wb");
pickle.dump(find_file_list,hd);
hd.close();
#====读取pick里面内容
hd = open(disk_file,"rb");
value = pickle.load(hd);

  

举报

相关推荐

python练习题(一)

python 练习题

python练习题

Python练习题

python 练习题-质数

Python列表练习题

【Python】函数练习题

0 条评论