我的代碼按順序排列。
drives = [ chr(x) ":\\" for x in range(65,91) if os.path.exists(chr(x) ":\\") ]
我使用此代碼塊查看指定磁盤中的所有檔案擴展名
ListFiles = os.walk("d:\\") #normally putting drives here. and getting an error.
SplitTypes = []
for walk_output in ListFiles:
for file_name in walk_output[-1]:
SplitTypes.append(file_name.split(".")[-1])
print(SplitTypes)
有了這個
counter = 0
inp = 'txt' #normally putting SplitTypes here and getting error
for drive in drives: # drops every .txt file that
for r, d, f in os.walk(drive): #It can get in every disk
for file in f: #(first block) get's every disk's available on system
filepath = os.path.join(r, file)
if inp in file: #this line find's every file that ends with .txt
counter = 1 #this line add's one and goes to the next one
print(os.path.join(r, file)) #every file' location gets down by down
print(f"counted {counter} files.") #this line finally gives the count number
第二個代碼塊列印出所有檔案的擴展名,例如:txt、png、exe、dll 等。
示例:
['epr',itx', 'itx', 'ilut', 'itx', 'itx', 'cube', 'cube', 'cube', 'itx', 'cube', 'cube''js','dll', 'dll', 'dll', 'json', 'json', 'json', 'json', 'json', 'json', 'json', 'json', 'json', 'json''rar', 'rar', 'ini', 'chm', 'dll', 'dll', 'dll', 'exe', 'sfx', 'sfx', 'exe', 'exe', 'ion', 'txt', 'txt', 'txt', 'exe', 'txt', 'txt', 'txt', 'txt',
'txt', 'txt', 'txt',]
我在這里面臨的問題是我無法掃描所有驅動程式中的擴展名(第二個代碼塊)。而且我無法搜索具有(第二個代碼塊)提供給第三個代碼塊的擴展名的所有檔案我正在構建自己的防病毒軟體
uj5u.com熱心網友回復:
以下將列印 Windows 系統每個磁盤上每個檔案的完整檔案路徑。請注意,可能需要很長時間才能完成。
import os
drives = [chr(x) ":\\" for x in range(65,91) if os.path.exists(chr(x) ":\\")]
counter = 0
for drive in drives:
for root, dirs, files in os.walk(drive):
for file in files:
print(os.path.join(root, file))
counter = len(files)
print(f"counted {counter} files.")
uj5u.com熱心網友回復:
OK完成!這是它如何完成洗掉不需要的第二個塊
這將遍歷每個可用的磁盤和檔案
糾正我如果這里有什么沒有意義我仍然是一個 python 新手
counter = 0
inp = []
for drive in drives:
for r, d, f in os.walk(drive):
for file in f:
filepath = os.path.join(r, file)
for inp in os.listdir(drive):
if inp in file:
counter = 1
print(os.path.join(r, file))
print(f"counted {counter} files.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483817.html
標籤:Python python-3.x 视窗
