python批量修改文件名的小例子

# coding=utf-8
import os,sys
import shutil
import struct

file_list = []

def listdir(folder, file_list):
    fileNum = 0
    new_file_list = os.listdir(folder) 
    for line in new_file_list:
        filepath = os.path.join(folder,line)
        if os.path.isfile(filepath):
            #print line
            file_list.append(line)
            fileNum = fileNum + 1
            #if fileNum > 10:
            #   return


#change .jpg.txt to .txt
def ChangeFileName(folder, file_list):
    for file_line in file_list:
        new_file_name = file_line.replace(".jpg.txt", ".txt")
        if new_file_name != file_line:
            print "file_name:" + file_line
            print "new_file_name:" + new_file_name
            os.rename(os.path.join(folder, file_line), os.path.join(folder, new_file_name))

folder = sys.argv[1]
print folder
listdir(folder, file_list)
ChangeFileName(folder, file_list)

#python this folder


蜗牛食堂

猜你喜欢

转载自huaxiamian.iteye.com/blog/2326300