Python批量重新命名檔案方法很簡單我們會利用listdir與rename 再加上目錄遍曆即可實現檔案重命令了,下面整理了一些方法。
用到了os的兩個介面:
1、列出檔案夾中的所有檔案(也包含目錄)
os.listdir(path)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Availability: Unix, Windows.
Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects
2、對檔案進行重新命名
os.rename(src, dst)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.
Availability: Unix, Windows
| 代碼如下 |
|
import os dirpath="D:/workbench/crazyant.net/myfiles" for fname in os.listdir(dirpath): newfname=fname[3:] newfpath="%s/%s"%(dirpath,newfname) oldfpath="%s/%s"%(dirpath,fname) os.rename(oldfpath, newfpath) |
其實就是用os.listdir讀取裡面所有的檔案,然後用os.rename進行檔案重新命名即可實現
3、對於上面的辦法我還找到一些方法
| 代碼如下 |
|
| # coding=utf-8 import os import re path = "D:temp" pattern = re.compile('d{3}') for file in os.listdir(path): if os.path.isfile(os.path.join(path, file)): match = pattern.search(file) assert match name = match.group() + '.mp3' #print file, name os.rename(os.path.join(path, file), os.path.join(path, name)) |
要注意match和search的區別:match是從頭開始匹配,search就是貪心。其實也沒這麼複雜有一個叫Bulk Rename Utility的玩意兒就是拿來重新命名用的。
再補充一個
輸入指定目錄,程式處理該目錄及其包含有big.jpg檔案的子目錄,
將名稱類似’big(5).jpg’的檔案改名為’big_5.jpg’,
即 big(N).jpg ==> big_N.jpg
代碼如下:
# coding: cp936
"""
檔案重新命名
輸入指定目錄,程式處理該目錄及其包含有big.jpg檔案的子目錄,
將名稱類似'big(5).jpg'的檔案改名為'big_5.jpg',
即 big(N).jpg ==> big_N.jpg
@author chenwei
@date 2010-12-26
"""
import glob
import os
def main():
print '重新命名任務開始'
base_dir = raw_input('請輸入待處理目錄:')
# 處理頂級目錄
rename_dir(base_dir)
# 尋找包含有big.jpg檔案的子目錄
pattern = base_dir + r'*big.jpg'
file_list = glob.glob(pattern)
# 對各目錄分別進行處理
for file in file_list:
i = file.rindex('')
dir = file[:i]
rename_dir(dir)
print '重新命名任務完成'
raw_input('輸入任意內容結束')
def rename_dir(dir):
# 根據模式尋找待改名的檔案
pattern = dir + r'big(*).jpg'
file_list = glob.glob(pattern)
# 分別處理各檔案
for file in file_list:
rename_file(file)
def rename_file(fname):
# 取得檔案名稱,去除路徑及副檔名
i = fname.rindex('')
j = fname.rindex('.')
basename = fname[i+1:j]
# 擷取檔案編號
k = basename.rindex('(')
l = basename.rindex(')')
seq = basename[k+1:l]
# 新檔案名稱
new_fname = fname.replace(fname[i+1:j], 'big_'+seq)
# 重新命名
try:
os.rename(fname, new_fname)
print '重新命名 ' + fname + ' to ' + new_fname + ' 成功'
except:
print '重新命名 ' + fname + ' to ' + new_fname + ' 失敗'
if __name__ == '__main__':
main()