【发布时间】:2022-06-11 03:11:23
【问题描述】:
我的代码按顺序排列。
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',]
我在这里面临的问题是我无法扫描所有驱动程序中的扩展(第二个代码块)。 而且我无法搜索具有(第二个代码块)提供给第三个代码块的扩展名的所有文件
【问题讨论】:
-
好的。 为什么您不能扫描所有驱动器中的扩展程序?是什么阻止了你?
-
检查第三个块 inp = SplitTypes 但无法打印出第二个块提供给第三个块的扩展 当我输入 inp='txt' 并注释时它通常应该打印出这些文件的文件位置out second block there is no problem 打印出以 .txt 结尾的文件的所有位置,但我想打印出每个扩展名。
-
此外,如果您查看最后一段代码,您会发现扩展正在重复,也许我们应该首先摆脱那里的重复值?
-
SplitTypes是文件扩展名的列表(可能有很多重复)。所以我不确定你认为if inp in file:做了什么,因为它永远是False(并且删除重复项并不能解决这个问题)。目前还不清楚首先获取所有扩展名的目的是什么,因为在第二个块中当然当前文件的扩展名将在“SplitTypes”中,因为它是每个看到的文件的列表。 -
注释掉第三个块并运行第二个块,因为它显示你会得到最后一个块
标签: python python-3.x windows