计算文件名的字符并重命名

时间:2016-02-22 08:12:44

标签: python-2.7

import os
import glob
from Tkinter import Tk
from tkFileDialog import askdirectory


fileDir = askdirectory()
print(fileDir)
#newfileDir=fileDir.replace('/','\\')

files =  glob.glob("*.ac3")
print files

cnt = 0
for f in files:
   if len(f) < 3 + 4:
      os.rename(f, str(cnt)+f)
      cnt += 1
      print("renamed " + f) 

我需要计算小于3个字符的文件名的字符长度,并将文件名重命名为numeric。

Here is error in this image

1 个答案:

答案 0 :(得分:0)

试试这段代码:

import glob
import os

files =  glob.glob("*.ac3")

cnt = 0
for f in files:
   if len(f) < 3 + 4: # 4 is the length of the extension ".ac3"
      os.rename(f, str(cnt)+f)
      cnt += 1
      print("renamed " + f)