Python:遍歷文件目錄及子目錄,並批量改變文件名稱


1、0.1版本

 1 import os
 2 
 3 
 4 def show_files(path, all_files):
 5     # 首先遍歷當前目錄所有文件及文件夾
 6     file_list = os.listdir(path)
 7     # 准備循環判斷每個元素是否是文件夾還是文件,是文件的話,把名稱傳入list,是文件夾的話,遞歸
 8     for file in file_list:
 9         # 利用os.path.join()方法取得路徑全名,並存入cur_path變量,否則每次只能遍歷一層目錄
10         cur_path = os.path.join(path, file)
11         # 判斷是否是文件夾
12         if os.path.isdir(cur_path):
13             # os.rename(cur_path, cur_path+'asdf')
14             # cur_path = cur_path+'asdf'
15             show_files(cur_path, all_files)
16         else:
17             """
18                 給每個文件重命名
19                 思路:把文件路徑用split()函數分割成列表,然后再修改最后的元素
20             """
21             name_all = cur_path.split('\\')
22 
23             # print(cur_path)
24             # print(name)
25             os.rename(cur_path, '\\'.join(name_all[0:-1])+'\\asdf'+name_all[-1])
26             all_files.append(file)
27 
28     return all_files
29 
30 
31 # 傳入空的list接收文件名
32 contents = show_files("e:\\python\\hello", [])
33 # 循環打印show_files函數返回的文件名列表
34 for content in contents:
35     print(content)

 

 

2、0.2版本

 1 import os
 2 
 3 
 4 """
 5     與01文件功能的不同,將改名封裝成函數
 6 """
 7 
 8 def show_files(path, all_files):
 9     # 首先遍歷當前目錄所有文件及文件夾
10     file_list = os.listdir(path)
11     # 准備循環判斷每個元素是否是文件夾還是文件,是文件的話,把名稱傳入list,是文件夾的話,遞歸
12     for file in file_list:
13         # 利用os.path.join()方法取得路徑全名,並存入cur_path變量,否則每次只能遍歷一層目錄
14         cur_path = os.path.join(path, file)
15         # 判斷是否是文件夾
16         if os.path.isdir(cur_path):
17             # os.rename(cur_path, cur_path+'asdf')
18             # cur_path = cur_path+'asdf'
19             show_files(cur_path, all_files)
20         else:
21             """
22                 給每個文件重命名
23                 思路:把文件路徑用split()函數分割成列表,然后再修改最后的元素
24             """
25             change_file_name(cur_path, 'ssss')
26             all_files.append(file)
27 
28     return all_files
29 
30 
31 def change_file_name(path_name, pre_fix):
32     old_name = path_name.split('\\')
33     print(old_name)
34     os.rename(path_name, '\\'.join(old_name[0:-1]) + '\\' + pre_fix + old_name[-1])
35     return
36 
37 
38 
39 
40 # 傳入空的list接收文件名
41 contents = show_files("e:\\python\\hello", [])
42 # 循環打印show_files函數返回的文件名列表
43 for content in contents:
44     print(content)

認真寫了三個小時,接觸python以來花時間學習最長的一次。

 

 

晚上再思考了一下,下邊的這種方法可能效率更高吧

 1 import os
 2 
 3 """
 4     另一種嘗試
 5 """
 6 
 7 
 8 def show_files(path, all_files):
 9     files = os.listdir(path)
10     # print(files)
11     for file in files:
12         # print(file)
13         if os.path.isdir(path + '/' + file):
14             # print(path + '/' + file)
15             show_files(path + '/' + file, all_files)
16         elif os.path.isfile(path + '/' + file):
17             os.rename(path + '/' + file, path + '/aaa' + file)
18             all_files.append(path + '/aaa' + file)
19 
20     return all_files
21 
22 
23 list_a = []
24 path = "e:/python/hello"
25 contents = show_files(path, list_a)
26 for content in contents:
27     print(content)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM