【问题标题】:Search through all drives for a specific file Python [duplicate]在所有驱动器中搜索特定文件 Python [重复]
【发布时间】:2017-10-23 10:15:37
【问题描述】:

我多次浏览了多个网站,没有其他帖子能找到我正在寻找的具体内容。

一般的想法是让程序搜索整个计算机,跳过任何拒绝访问或以其他方式无法访问的文件夹,查找特定文件,然后返回其位置。例如:

def findFile(targetFile):
    targetLocation = searchFor(targetFile) in drives.All

OUTPUT:

targetLocation = "D:\SteamLibrary\steamapps\common\Grand Theft Auto V"

OR:

This file exists in two locations:
"D:\SteamLibrary\steamapps\common\Grand Theft Auto V" and "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V"
Please specify which:

谢谢

注意:我已经看到 questions/1724693/find-a-file-in-python 并没有回答我的问题。我需要一些可以通过要求您选择不将其放入数组中来处理重复项的东西

import os
target_filename = "GTA 5.exe"
start_folder = "C:/" #It needs to search every drive (C: D: E: F: etc)
for root, dirs, files in os.walk(start_folder):
    if target_filename in files:
        print(root + '\\' + target_filename )

输出如下所示:

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)]     on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================== RESTART: C:/Users/Redacted/Desktop/test.py ==================
>>> 

【问题讨论】:

标签: python python-3.x


【解决方案1】:

如果您知道要使用 os.walk 查找重复项的文件名(假设为 target_filename)和起始文件夹(假设为 start_folder):

import os

for root, dirs, files in os.walk(start_folder):
    if target_filename in files:
        print(root + '\\' + target_filename )

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2011-10-23
    相关资源
    最近更新 更多