环境
windows, python, os库
代码
import os
def renameFiles(folderName):
'''
Rename files in the given folder;
'''
listFile = os.listdir(foldername)
for f in listFile:
if f.endswith(".jpg"):
f_ = f[:-4] + "_806d6.jpg"
oldname = foldername + "/" + f
newname = foldername + "/" + f_
os.rename(oldname, newname)
return 0
def copyFiles(folderOld, folderNew):
'''
copy files from folderOld to folderNew;
'''
listFile = os.listdir(folderOld)
for f in listFile:
if f[-2] == 'p':
copyCmd = "copy " + folderOld + "\\" + f + " " + folderNew
os.system(copyCmd)
return 0
def delFiles(folderName):
'''
remove files in folder;
'''
listFile = os.listdir(folderName)
for f in listFile:
#if "w" in f:
if f.endswith("806.jpg") or f.endswith("806d.jpg") or f.endswith("806d2.jpg") or f.endswith("806d3.jpg"):
a_target = foldername + "/" + f
os.remove(a_target)
return 0
def moveFiles(folderOld, folderNew):
'''
move files from folderOld to folderNew;
'''
listFile = os.listdir(folderOld)
for f in listFile:
if f.endswith(".jpg"):
moveCmd = "move " + folderOld + "\\" + f + " " + folderNew
os.system(moveCmd)
return 0
def main():
folder_1 = "compare0929"
renameFiles(folder_1)
if __name__ == '__main__':
main()