CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
文件处理进阶
import os
import re
import xlwt
import shutil
提取文件
def copyf(path1 , path2):
for file in os.scandir(path1):
if (file.is_dir()) and (path1.find('114')>0 ):
path11=os.path.join(path1,file)
for file in os.scandir(path11):
if (file.is_dir()) and (path11.find('11')>0 ):
path111=os.path.join(path11,file)
for file in os.listdir(path111):
if re.search('GazeFix.csv',file):
File=os.path.join(path111,file)
target=os.path.join(path2,file)
shutil.copyfile(File,target)
copyf("D:\\my content\\liyan\\Desktop\\New folder\\114" , "D:\\my content\\liyan\\Desktop\\New folder\\diary")
移动文件
def movef(path1 , path2):
for file in os.scandir(path1):
if (file.is_dir()) and (path1.find('114')>0 ):
path11=os.path.join(path1,file)
for file in os.scandir(path11):
if (file.is_dir()) and (path11.find('11')>0 ):
path111=os.path.join(path11,file)
for file in os.listdir(path111):
if re.search('GazeFix.csv',file):
File=os.path.join(path111,file)
target=os.path.join(path2,file)
shutil.move(File,target)
movef("D:\\my content\\liyan\\Desktop\\New folder\\114" , "D:\\my content\\liyan\\Desktop\\New folder\\diary")
删除文件
def removef(path1 ):
for file in os.scandir(path1):
if (file.is_dir()) and (path1.find('114')>0 ):
path11=os.path.join(path1,file)
for file in os.scandir(path11):
if (file.is_dir()) and (path11.find('11')>0 ):
path111=os.path.join(path11,file)
for file in os.listdir(path111):
if re.search('.csv',file):
File=os.path.join(path111,file)
os.remove(File)
removef("D:\\my content\\liyan\\Desktop\\New folder\\114" )
重命名文件
def renamef(path1):
i=0
for file in os.scandir(path1):
if (file.is_dir()) and (path1.find('114')>0 ):
path11=os.path.join(path1,file)
for file in os.scandir(path11):
if (file.is_dir()) and (path11.find('11')>0 ):
path111=os.path.join(path11,file)
for file in os.listdir(path111):
if re.search('Userinfo.csv',file):
i+=1
File=os.path.join(path111,file)
target=os.path.join(path2,file)
os.rename(File,os.path.join(path111,str(i)+".csv"),)
renamef("D:\\my content\\liyan\\Desktop\\New folder\\114" )