python批量檔案重新命名

2022-09-25 00:24:08 字數 777 閱讀 7619

# -*- coding: utf-8 -*-

import os, sys

# python根據條件修改目錄裡的檔名:將不想要的刪去或者替換掉

# 設定檔案路徑

# path = 'e:\\python\\changefilename\\miniteco\\'

path = 'd:\\documents\\matlab\\978-7-302-46737-3matlab\\chapter12\\'

def rename(path):

# 對目錄下的檔案進行遍歷

for file in os.listdir(path):

# 判斷是否是檔案(查詢以ql開頭以.rmvb結尾的檔案)

#if (file.startswith("ql") and file.endswith(".rmvb")):

if (file.startswith("ex") and file.endswith(".m")):

print(file)

# 設定新檔名

# newname=file.replace("ql","")#這一句的效果是直接刪除ql

newname = file.replace("-", "") # 這一句的效果是將-替換為空

# 重新命名

os.rename(os.path.join(path, file), os.path.join(path, newname))

rename(path)

# 結束

print("end")

python 批量重新命名檔案

利用python可以對檔案進行批量重新命名 下面是 也比較簡單,讀者可以根據自己的需要自行修改。將i盤裡的3資料夾裡的檔案從302開始重新命名 import os def rename i 301 path i 3 windows系統用雙斜線 filelist os.listdir path for...

python批量檔案重新命名

某個目錄下存在大量,檔名是檔案hash,檔名太長訪問比較麻煩 注意 用到了f string,需要python3.6 可去除f string.另外不要重複執行此段 否則相同的命名規則會直接覆蓋同名的檔案,導致檔案減少,看上去就像被刪除了.如已存在img1.jpg,重新命名將img2.jpg命名為img...

批量重新命名檔案

windows xp下 比如 ren jpe jpg linux下 在多數系統中,rename是乙個perl指令碼,rename的使用也支援perl的正規表示式。rename的基本語法是 rename perl表示式 檔名perl表示式用於修改檔名,如s開頭的串表示替換。舉幾個比較實用的例子 刪除所...