Created
April 22, 2017 09:31
-
-
Save guandaxia/0ba4a085da9a7aafd34741e7fed48121 to your computer and use it in GitHub Desktop.
通过给定的文件名来重命名文件夹中的文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xlrd | |
import os; | |
import shutil; | |
ids = [] | |
def readId(): | |
filename = "C:\\Users\\administere\\Documents\\Tencent Files\\FileRecv\\签到表 2.xlsx" | |
data = xlrd.open_workbook(filename) | |
table = data.sheet_by_name(u'Sheet1 (17)')#通过名称获取 | |
info = table.col_values(2) | |
i=0 | |
for id in info: | |
i=i+1 | |
if(i<5): | |
continue | |
ids.append(str(int(id))) | |
def rename(): | |
oldPath="C:\\Users\\administere\Desktop\\test" | |
newPath="C:\\Users\\administere\Desktop\\new" | |
if(not os.path.exists(newPath)): | |
os.makedirs(newPath) | |
# shutil.copytree(oldPath, newPath) | |
filelist=os.listdir(oldPath)#该文件夹下所有的文件(包括文件夹) | |
i=0 | |
for files in filelist:#遍历所有文件 | |
olddir=os.path.join(oldPath,files) #原来的文件路径 | |
if os.path.isdir(olddir):#如果是文件夹则跳过 | |
continue; | |
filename=os.path.splitext(files)[0]#文件名 | |
filetype=os.path.splitext(files)[1]#文件扩展名 | |
newdir=os.path.join(newPath,"A"+ids[i]+filetype)#新的文件路径 | |
os.rename(olddir,newdir)#重命名 | |
shutil.copyfile(newdir,olddir) | |
newdir=os.path.join(newPath,"B"+ids[i]+filetype)#新的文件路径 | |
os.rename(olddir,newdir)#重命名 | |
shutil.copyfile(newdir,olddir) | |
i = i+1 | |
print("修改成功") | |
readId() | |
print(ids) | |
rename(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment