python3 實現對資料夾中的檔案批量重新命名

2021-08-24 20:53:23 字數 564 閱讀 1712

# 對於資料夾下的所有檔案重新命名,會生成多餘0檔案,刪掉即可

import os

path = 'img' # 對該路徑下所有檔案重新命名排序

count = 0

file_list = os.listdir(path) # 該資料夾下所有的檔案(包括資料夾)

forfile

in file_list: # 遍歷所有檔案

olddir=os.path.join(path, file) # 原來的檔案路徑

if os.path.isdir(olddir): # 如果是資料夾則跳過

continue

filename=os.path.splitext(file)[0] # 檔名

filetype=os.path.splitext(file)[1] # 副檔名

newdir=os.path.join(path, str(count) + filetype) # 新的檔案路徑

os.rename(olddir, newdir) # 重新命名

count += 1

python中對檔案 資料夾的操作

python中對檔案 資料夾的操作需要涉及到os模組和shutil模組。建立檔案 1 os.mknod test.txt 建立空檔案 2 open test.txt w 直接開啟乙個檔案,如果檔案不存在則建立檔案 建立目錄 os.mkdir file 建立目錄 複製檔案 shutil.copyfil...

python中對檔案 資料夾的操作

python中對檔案 資料夾的操作需要涉及到 os模組和 shutil 模組。建立檔案 1 os.mknod test.txt 建立空檔案 2 open test.txt w 直接開啟乙個檔案,如果檔案不存在則建立檔案 建立目錄 os.mkdir file 建立目錄 複製檔案 shutil.copy...

python中對檔案 資料夾的操作

python中對檔案 資料夾的操作需要涉及到os模組和shutil模組。建立檔案 1 os.mknod test.txt 建立空檔案 2 open test.txt w 直接開啟乙個檔案,如果檔案不存在則建立檔案 建立目錄 os.mkdir file 建立目錄 複製檔案 shutil.copyfil...