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

时间:2016-05-25 04:10:22

标签: python python-3.x python-2.7

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

如果没有该模块,我必须列出当前目录中的所有文件,搜索有问题的文件,如果文件不存在,则递归上移。有更简单的方法吗?

8 个答案:

答案 0 :(得分: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 :(得分:1)

import glob并使用glob.glob('your_pattern_or_name_of_file') 你可以在这里看到关于glob的文档 https://docs.python.org/2/library/glob.html

答案 2 :(得分:1)

这是一个示例,它将查找指定目录中的所有.csv文件" path"及其所有根目录并打印出来:

    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()   

答案 3 :(得分:0)

我想说你可以使用glob.glob()找到你要查找的所有文件。glob模块根据Unix shell使用的规则查找匹配指定模式的所有路径名,虽然结果以任意顺序返回。来自文档 -

  

glob.glob(pathname,*,recursive = False)

     

返回可能为空的列表   与pathname匹配的路径名,必须是包含a的字符串   路径规范。 pathname可以是绝对的(比如   /usr/src/Python-1.5/Makefile)或亲戚(比如../../ Tools / * / * .gif),   并且可以包含shell样式的通配符。包含损坏的符号链接   结果(如在shell中)。

说,我们的目标是从目录,子目录及其父目录中查找所有文本文件。使用os.walk()os.chdir()转到要使用的目录。所以我转到了我当前的工作目录,然后可以使用此代码段访问所有文本文件 -

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')

答案 4 :(得分:0)

刚写了这个来找到“images”目录,注意'/'是Linux风格

datesandcontent = [elm.get_text() for elm in soup.find_all('div', {'class' : "review-content"})]
dates = [re.search(r'(\d+/\d+/\d+)', elm).group(1) for elm in datesandcontent]
dates2 = [datetime.strptime(date, '%m/%d/%Y') for date in dates]
dates3 = [datetime.strftime(date2, '%Y-%m-%d') for date2 in dates2]

答案 5 :(得分: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)

答案 6 :(得分: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')或其他一些字符串比较。

答案 7 :(得分:0)

这里有一个向上搜索的函数:

import sys, os, os.path
def up_dir(match,start=None):
    """
    Find a parent path producing a match on one of its entries.
    Without match an empty string is returned.

    :param match: a function returning a bool on a directory entry
    :param start: absolute path or None
    :return: directory with a match on one of its entries

    >>> up_dir(lambda x: False)
    ''

    """

    if start is None:
        start = os.getcwd()
    if any(match(x) for x in os.listdir(start)):
        return start
    parent = os.path.dirname(start)
    if start == parent:
        rootres = start.replace('\\','/').strip('/').replace(':','')
        if len(rootres)==1 and sys.platform=='win32':
            rootres = ''
        return rootres
    return up_dir(match,start=parent)