//批量修改文件名称
import os
dir = os.getcwd()
dir = os.chdir(r'C:\Users\Administrator\Desktop\cupdata')
j = 0
for file in os.listdir(dir):
if file[-2: ] == 'py':
continue
os.rename(file,str(j)+'.jpg')
j+=1
print (file)
//修改文件名称的同时将文件名称写入txt文件中
import os
dir = os.getcwd()
dir = os.chdir(r'C:\Users\Administrator\Desktop\img')
f = open(r'C:\Users\Administrator\Desktop\bg.txt','a')
j = 0
for file in os.listdir(dir):
if file[-2: ] == 'py':
continue
os.rename(file,'bg'+str(j)+'.jpg')
f.write('img/bg'+str(j)+'.jpg' + '\n')
j+=1
print (file)