【问题标题】:Can't print filepath of all files on all drives无法打印所有驱动器上所有文件的文件路径
【发布时间】: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


【解决方案1】:

下面将打印 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.")

【讨论】:

  • 比你好多了!这就是我一直在寻找的
  • 但是我仍然有一个问题,这只花了 5 分钟,但我不确定它是否找到了所有内容当我搜索 .png 时,我看不到任何内容如何验证这是否真的有效?
  • 没有理由跳过 .png(或任何其他类型的)文件。为了使测试更容易,drives 列表的元素可以更改为特定驱动器上的特定目录:即drives = [r'c:\testdirs\dir1', r'd:\path\to\another\dir 等...]`。
  • 这没有得到我得到的每个文件超过 800.000 个文件的批处理脚本只在 c:\ 如果你把 d:\ 也放在运行它时它会超过一百万只是在两个驱动器中打印出 600.000
  • 正如我所说,我不知道任何文件被跳过的原因。
【解决方案2】:

好的!这是如何完成删除不需要的第二个块
这将遍历所有可用的磁盘和文件
纠正我如果这里有什么不合理的地方,我仍然是 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.")

【讨论】:

  • 我同意你不需要第二块。但是,此代码也不起作用。 if inp in file: 仍然没有意义,并且只有在文件恰好包含其所在目录的名称(即J:\Thunderbird\Profiles\bob\dictionarysearchThunderbird3Overlay.xul)时才会成为True
  • 我认为您需要在代码块中解释您要完成的工作——它似乎与问题的标题没有任何关系。
  • 我想找到磁盘中的每个文件夹并搜索所有文件
猜你喜欢
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 2017-04-03
  • 1970-01-01
相关资源
最近更新 更多