使用python把資料夾下的檔案順序命名

2021-10-03 05:51:59 字數 733 閱讀 4392

# --** coding="utf-8" **--

import os

import re

import sys

filelist = os.listdir(r"d:\project\cow_face\image\train"

)print

("start..."

)# 得到程序當前工作目錄

currentpath = os.getcwd(

)# 將當前工作目錄修改為待修改資料夾的位置

os.chdir(r"d:\project\cow_face\image\train"

)# 名稱變數

num =

1# 遍歷資料夾中所有檔案

for filename in filelist:

# 檔案重新命名

# 改變編號,繼續下一項

num = num +

1print

("end..."

)# 改回程式執行前的工作目錄

os.chdir(currentpath)

# 重新整理

sys.stdin.flush(

)

如果發生重名現象,比如已經有了部分的檔案名字,可以嘗試把num設定為1000或者10000,這樣可以清除原來的檔名。

Python如何import資料夾下的檔案

python的import包含檔案功能就跟php的include類似,但更確切的說應該更像是php中的require,因為python裡的import只要目標不存在就報錯程式無法往下執行。要包含目錄裡的檔案,php中只需要給對路徑就ok。python中則不同,下面來看看這個例子。目錄結構 a.py ...

python 統計資料夾下的所有資料夾或檔案數目

統計 home jiangt下資料夾的個數 import os path home jiangt count 0 for file in os.listdir path file 表示的是檔名 count count 1 print count獲取資料夾下的檔案的個數 import os path ...

用python計算大資料夾下小資料夾裡檔案的個數

coding utf 8 計算人臉集中每個資料夾裡的個數 編輯器為python2.7 import os count1 0 計數大資料夾下共有多少個小資料夾 for filename in os.listdir f 2017110215 machinevision 22 print filename...