Python: finding a file in the current directory and all its parents

Is there a built-in module to search for a file in the current directory, as well as all the super directories?

Without the module, I will have to list all the files in the current directory, look for the file in question, and move recursively if the file is missing. Is there an easier way to do this?

+7
source share
7 answers

Well, this is not so well implemented, but will work

use listdir to get a list of files / folders in the current directory, and then find your file in the list.

If it exists, loop breaks, but if it does not exist, it goes to the parent directory using os.path.dirname and listdir .

if cur_dir == '/' parent dir for "/" returned as "/" so if cur_dir == parent_dir this breaks the loop

 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 
+5
source

Here is an example that will find all .csv files in the specified "path" directory and all its root directories and print them:

  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) 

If you want to start from one directory and walk through your parents, this will help you find all the .csv files (for example):

 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() 
+1
source

I would say that you can use glob.glob() to search for all the files you are looking for. The glob module finds all pathnames matching the given pattern according to the rules used by the Unix shell, although the results are returned in random order. From the documents -

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

Return a possibly empty list of path names that match the path name, which should be a string containing the path. pathname can be either absolute (e.g. / usr / src / Python -1.5 / Makefile) or relative (e.g. .. / .. / Tools / * / *. gif) , and can contain shell-style wildcards. Broken symbolic links are included in the results (as in the shell).

Let's say our goal is to find all the text files from the directory, its subdirectories and the parent directory. Use os.walk() or os.chdir() to go to the directory you want to work with. So I went to my current working directory and from there I could access ALL text files with this piece of code -

 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) 

I get all text files (54) from all subdirectories. This conclusion shows a few -

 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 

To get text files from the parent directory (and its direct subdirectories), just change this to arr=glob.glob('..\*\*.txt')

0
source

import glob and use glob.glob('your_pattern_or_name_of_file') Here you can see the glob documentation https://docs.python.org/2/library/glob.html

0
source

Just wrote this to find the "images" directory, note: "/" - Linux style

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

The parent question was to go through the parent directories (don't descend into descendants like the 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) 

Here is a version that uses shell globalization to match multiple files:

 # 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
source

I also looked for this, since os.walk is exactly the opposite of what I wanted. It is looking for subdirectories. I wanted to search the parent directories in the opposite direction until I hit the root of the disk.

The frantic inspiration from the previous answers is what I use below. It does not require changing the working directory, and you have a place where you can do something when you find a match. And you can change the way you look for a match. I use regex, but basic string comparison is fine too.

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

Other comparisons you could make are item.lower().endswith('.txt') or some other string comparison.

0
source

All Articles