无法打印所有驱动器上所有文件的文件路径
Can't print filepath of all files on all drives
我的代码顺序到行。
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',]
我在这里面临的问题是我无法扫描所有驱动程序中的扩展(第二个代码块)。
而且我无法使用(第二个代码块)提供给第三个代码块的扩展名搜索所有文件
我正在构建自己的防病毒软件
好的!这是它的完成方式删除不需要的第二个块
这将遍历所有可用的磁盘和文件
纠正我如果这里有什么不明白我仍然是一个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.")
以下将打印 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.")
我的代码顺序到行。
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',]
我在这里面临的问题是我无法扫描所有驱动程序中的扩展(第二个代码块)。 而且我无法使用(第二个代码块)提供给第三个代码块的扩展名搜索所有文件 我正在构建自己的防病毒软件
好的!这是它的完成方式删除不需要的第二个块
这将遍历所有可用的磁盘和文件
纠正我如果这里有什么不明白我仍然是一个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.")
以下将打印 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.")