code1:file rename©&delete
import os
import re
pathPre = ".\TT_"
pathPostList = ["C2","C3","C4","C5","C6"]
str1Pre= "FX11_TT_"
print("Please enter cmd for script")
print("1: change file name")
print("2: copy files")
print("3: delete files")
Parameter = input('please enter parameter:')
print("-----------------------------------------")
if Parameter == 1:
print("change file name:")
pattern = r'FX11_TT_C1'
for pathPost in pathPostList:
path = pathPre + pathPost
str1 = str1Pre + pathPost
#get all file name in the folder
fileList=os.listdir(path)
#fileList=os.walk(path)
for i in fileList:
#print(i)
oldname=path+ os.sep + i
#print("oldname:",oldname)
newname=re.sub(pattern,str1,i)
#print(newname)
newname = path+ os.sep + newname
#print("newname:",newname)
#print(" \n")
if oldname == newname:
oldname = newname + "123"
else:
print("oldname:",oldname)
print("newname:",newname)
print(" \n")
os.rename(oldname,newname)
print("-----------------------------------------")
elif Parameter == 2:
print("copy files:")
pattern = r'.png'
pathSrc = pathPre + "C1"
fileList=os.listdir(pathSrc)
for file in fileList :
fileFullNameSrc = pathSrc + os.sep + file
for pathPost in pathPostList:
pathDir = pathPre + pathPost
fileFullNameDir = pathDir + os.sep + file
searchResult = re.search(pattern,fileFullNameSrc)
if searchResult :
copycmdStr = "xcopy " + fileFullNameSrc + " " + pathDir
print(copycmdStr)
os.system(copycmdStr)
#os.system("xcopy C:\1.txt D:")
print("-----------------------------------------")
elif Parameter == 3:
print("delete files:")
pattern = r'.png'
for pathPost in pathPostList:
path = pathPre + pathPost
fileList=os.listdir(path)
for file in fileList:
fileFullName = path+ os.sep + file
searchResult = re.search(pattern,fileFullName)
if searchResult :
os.remove(fileFullName)
print("delete :"+fileFullName)
print("-----------------------------------------")
else:
print("parameter Err")
print("-----------------------------------------")
code2:delete all special file
import os
import re
import sys
import glob
path = "."
pattern = r'_alternativ'
str1= "FX11_TT_"
#get all file name in the folder
#fileList=os.walk(path)
def searchAllFile(inDir):
#fileList=os.listdir(inDir)
fileList=os.walk(inDir)
for i in fileList:
#print("-------------------start----------------------")
#print(i)
localpath = i[0]
#print(i[0])
for file in i[2]:
#print(file)
localFileFullName = localpath + os.sep + file
searchResult = re.search(pattern,file)
if searchResult :
os.remove(localFileFullName)
print("delete :"+localFileFullName)
searchAllFile(path)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)