Python:在当前目录及其所有父目录中搜索文件

问题描述 投票:7回答:7

是否有内置模块可以在当前目录以及所有超级目录中搜索文件?

没有模块,我将不得不列出当前目录中的所有文件,搜索有问题的文件,如果不存在该文件,则递归地向上移动。有没有更简单的方法可以做到这一点?

python python-3.x python-2.7
7个回答
5
投票

嗯,这不是很好实现,但是会起作用

使用listdir获取当前目录中的文件/文件夹列表,然后在列表中搜索文件。

如果存在,则循环中断,但如果不存在,则使用os.path.dirnamelistdir转到父目录。

如果cur_dir == '/' "/"的父目录返回为"/",那么如果cur_dir == parent_dir会中断循环

import os
import os.path

file_name = "test.txt" #file to be searched
cur_dir = os.getcwd() # Dir from where search starts can be replaced with any path

while True:
    file_list = os.listdir(cur_dir)
    parent_dir = os.path.dirname(cur_dir)
    if file_name in file_list:
        print "File Exists in: ", cur_dir
        break
    else:
        if cur_dir == parent_dir: #if dir is root dir
            print "File not found"
            break
        else:
            cur_dir = parent_dir

1
投票

这里是一个示例,它将在指定目录“路径”及其所有根目录中找到所有.csv文件,并打印它们:

    import os
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(".csv"):
                path_file = os.path.join(root,file)
                print(path_file)

如果您想从一个目录开始并通过父目录工作,则可以找到所有.csv文件(例如):

import os
import glob
last_dir = ''
dir = r'c:\temp\starting_dir'

os.chdir(dir)
while last_dir != dir:
    dir = os.getcwd()
    print(glob.glob('*.csv'))
    os.chdir('..')
    last_dir = os.getcwd()   

0
投票

import glob并使用glob.glob('your_pattern_or_name_of_file')您可以在此处查看有关glob的文档https://docs.python.org/2/library/glob.html


0
投票

我想说您可以使用glob.glob()查找所有要查找的文件。glob模块会根据Unix Shell使用的规则查找与指定模式匹配的所有路径名,尽管会在任意顺序。从文档-

glob.glob(路径名,*,递归= False)

返回可能为空的列表匹配路径名的路径名,该路径名必须是包含路径规范。路径名可以是绝对值(例如/usr/src/Python-1.5/Makefile)或相对版本(如../../ Tools / * / *。gif),并可以包含外壳样式的通配符。损坏的符号链接包含在结果(如在外壳中)。

说,我们的目标是从目录,子目录和父目录中查找所有文本文件。使用os.walk()os.chdir()进入要使用的目录。所以我转到当前的工作目录,然后可以从该目录访问ALL文本文件,并带有以下代码片段-

import glob
arr=glob.glob('*\*\*.txt') 

'''....thesis/tweets is the path I walked to which
   has further sub directories, tweets\LDA on tweets\test file for main reults ,
tweets\LDA on tweets\paris_tweet ,tweets\LDA on tweets\hurricane_patricia\ '''

count=0
for filename in arr:
    print (filename)
    count+=1
print("ran successfulyy!!!! count = ",count)

我从所有子目录中获取所有文本文件(54)。此输出仅显示一些-

LDA on tweets\paris_tweet\ldaparisresults.txt
LDA on tweets\paris_tweet\ldaparisresults1.txt
LDA on tweets\hurricane_patricia\80,xldahurricaneresults.txt
LDA on tweets\hurricane_patricia\entitieshurricane.txt
LDA on tweets\test file for main reults\80,10ldamainresults.txt
LDA on tweets\test file for main reults\80,30ldamainresults.txt

要从父目录(及其直接子目录)获取文本文件,只需将其更改为arr=glob.glob('..\*\*.txt')


0
投票

只是写这个来找到“ images”目录,注意'/'是Linux样式

dir = os.getcwd()
    while dir != '/' and not glob.glob( dir + '/images' ):
        dir = os.path.dirname(dir)

0
投票

父级问题是遍历[[parent目录(不像find命令那样进入子级):

# walk PARENT directories looking for `filename`: f = 'filename' d = os.getcwd() while d != "/" and f not in os.listdir(d): d = os.path.abspath(d + "/../") if os.path.isfile(os.path.join(d,f)): do_something(f)
这里是使用shell globbing来匹配多个文件的版本:

# walk PARENT directories looking for any *.csv files, # stopping when a directory that contains any: f = '*.csv' d = os.getcwd() while d != "/" and not glob.glob(os.path.join(d, f)): d = os.path.abspath(d + "/../") files = glob.glob(os.path.join(d,f)) for filename in files: do_something(filename)


0
投票
我也在寻找这个,因为os.walk与我想要的完全相反。搜索子目录。我想向后搜索父目录,直到找到驱动器根目录。

[从以前的答案中汲取灵感,以下是我正在使用的方法。它不需要更改工作目录,并且在找到匹配项时可以放一些东西。您可以更改找到匹配项的方式。我正在使用正则表达式,但是基本的字符串比较也可以正常工作。

# Looking for a file with the string 'lowda' in it (like beltalowda or inyalowda) import os import re # only if you want to use regex # Setup initial directories starting_dir = 'C:\\Users\\AvasaralaC\\Documents\\Projects' last_dir = '' curr_dir = starting_dir filename = '' # Loop through parent directories until you hit the end or find a match while last_dir != curr_dir: for item in os.listdir(curr_dir): if re.compile('.*lowda.*').search(item): # Here you can do your own comparison filename = (curr_dir + os.path.sep + item) break if filename: break last_dir = curr_dir curr_dir = os.path.abspath(curr_dir + os.path.sep + os.pardir)

您可以进行的其他比较是item.lower().endswith('.txt')或其他一些字符串比较。
© www.soinside.com 2019 - 2024. All rights reserved.