标签:with basename star __file__ mil family def new ==
import os
def putName(path, flag, oldName, newName):
"""
修改文件名称或者替换文件名中的内容
"""
dirList = os.listdir(path)
for i in dirList:
if i.startswith(‘.‘):
continue
newPath = os.path.join(path, i)
if os.path.isdir(newPath):
putName(newPath, flag, oldName, newName)
elif os.path.isfile(newPath):
if flag == ‘1‘:
if os.path.basename(newPath) == oldName:
fileName = os.path.join(os.path.dirname(newPath), newName)
os.rename(newPath, fileName)
print(‘Done!‘)
else:
print(‘没有发现要修改的文件名‘)
elif flag == ‘0‘:
if os.path.basename(newPath).startswith(oldName):
os.rename(newPath, os.path.join(os.path.dirname(newPath), os.path.basename(newPath).
replace(oldName, newName)))
print(‘Done!‘)
else:
print(‘没有发现要修改的文件名‘)
path = os.path.join(os.path.dirname(__file__), ‘../../Desktop‘)
flag = input(‘修改文件名称输入1,批量替换文件名内容输入0:‘)
if flag == ‘1‘:
oldName = input(‘输入要修改的文件名:‘)
newName = input(‘输入修改后的文件名:‘)
putName(path, flag, oldName, newName)
elif flag == ‘0‘:
oldName = input(‘输入要修改的文件名部分:‘)
newName = input(‘输入替换后的文件名部分:‘)
putName(path, flag, oldName, newName)
else:
print(‘输入有误,请重新输入‘)
标签:with basename star __file__ mil family def new ==
原文地址:https://www.cnblogs.com/changqing8023/p/9068455.html