In my code, I'm able to search through a directory for a file with a specific name. How could I edit the code so that it first searches for folders with names ending with the word "output". In the below code, when I tried to include the commented out line, the whole thing refused to run. Any help on what I'm missing would be greatly appreciated
def test():
file_paths = []
filenames = []
for root, dirs, files in os.walk('/Users/Bashe/Desktop/121210 p2/'):
for file in files:
#if re.match("output", file):
if re.match("Results",file):
file_paths.append(root)
filenames.append(file)
return file_paths, filenames
To search files that are in a folder with name ending with the word "output":
for dirpath, dirs, files in os.walk(rootdir):
if not dirpath.endswith('output'):
continue # look in the next folder
# search files
for file in files: ...
If you don't want even to visit any directories that are not ending with "output":
if not rootdir.endswith('output'):
return # do not visit it at all
for dirpath, dirs, files in os.walk(rootdir):
assert dirpath.endswith('output')
dirs[:] = [d for d in dirs if d.endswith('output')] # edit inplace
# search files
for file in files: ...
Related
I have following script which is supposed to list all picture etc from my disk drives, but it failes to find any file. Any Idea what I may be doing wrong. I am doing in PyCharm environment:
files = []
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith((".jpg", ".jpeg", ".mp3", ".mp4")):
files.append(os.path.join(root, file))
# Sort the files by their current folder name
files.sort(key=lambda x: os.path.dirname(x))
# Display a list of the files
for file in files:
print(file)
Your for-loop will never end as you add the found file name into files which you are looping through. Use another variable to store the list.
This works fine:
import os
results = []
for root, dirs, files in os.walk("."):
print(f"Got: {len(results)} files, current dir: {root}")
for file in files:
if file.endswith((".jpg", ".jpeg", ".mp3", ".mp4")):
results.append(os.path.join(root, file))
# Sort the files by their current folder name
results.sort(key=lambda x: os.path.dirname(x))
# Display a list of the files
for file in results:
print(file)
I would like to know how I can only append sub-directories of a given directory to a list in Python.
Something like this is not what I'm searching for as it outputs the files:
filelist = []
for root, dirs, files in os.walk(job['config']['output_dir']):
for file in files:
# append the file name to the list
filelist.append(os.path.join(root, file))
# print all the file names
for name in filelist:
print(name)
Just use the dirs variable instead the files variable, like this:
dirs_list = []
for root, dirs, files in os.walk('test'):
for dir in dirs:
dirs_list.append(os.path.join(root, dir))
print(dirs_list)
In a directory, I am searching for files that meet a certain condition. I am using code provided by an other stackoverflow post, but it's sending me into an infinite loop for a reason I can't see.
I iterate through directories in a subdirectory. Then, I iterate through the subsubdirectories in the subdirectory, looking for ones that meet a condition. Here is my code:
import os
rootdir ="/mnt/data/SHAVE_cases"
cases = []
for subdirs, dirs, files in os.walk(rootdir):
for subdir in subdirs:
print("1")
# now we are in the file subdirectory.
# we need to see if these subsubdirectories start with "multi"
for subdirs2, d, f in os.walk(rootdir + subdir):
for s in subdirs2:
if s.startswith("multi"):
cases.append(s)
Here is another code following the advice of msi
for pathname, subdirs, files in os.walk(rootdir):
for subdir in subdirs:
for path, sub, f in os.walk(rootdir + subdir):
print(sub)
It still returns an infinite loop.
I solved this using the answer from this question.
To search through the subdirectories for a name, we use find_dir().
import os
def find_dir(name, start):
for root, dirs, files in os.walk(start):
for d in dirs:
if d == name:
return os.path.abspath(os.path.join(root, d))
I've changed my code completely. Here is a piece of it, so you can see find_dir() in action.
for key in dictionary:
name = "goal_dir"
if find_dir(name, key) != None:
# perform analysis on the directory
I want to iterate subdirectories within a directory and get all of the config.json files at the root of each subdirectory. I am having trouble with stopping the walk function at the first subdirectory. An example of my folder structure is //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1 is the rootdir while I want to get config.jsons out of folders like this //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1/website1/config.json //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1/website2/config.json
//abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1/website3/config.json
Here is the code that I have been working with; it returns all folders within the rootdir and gives me an IOError. How do I stop the iteration and resolve the IOError?
import os
rootdir = r'//abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1'
for subdir, dirs, files in os.walk(rootdir):
for dir in dirs:
print dir
I think you want something like this.
import os
rootdir = r'//abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1'
for subdir, dirs, files in os.walk(rootdir):
if 'config.json' in files:
dirs[:] = []
print(os.path.join(rootdir, subdir, 'config.json')
I have the below code to print the filename which is find criteria with file extension *.org. How could I print the relative path of the file found. Thanks in advance
def get_filelist() :
directory = "\\\\networkpath\\123\\abc\\"
filelist = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('Org'):
print(str(dirs) +"\\" + str(file)) #prints empty list [] followed by filename
filelist.append(os.path.splitext(file)[0])
return (filelist)
Please see me as novice in python
files and dirs list the children of root. dirs thus lists siblings of file. You want to print this instead:
print(os.path.relpath(os.path.join(root, file)))
you need to use os.path.join:
def get_filelist() :
directory = "\\\\networkpath\\123\\abc\\"
filelist = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('org'): # here 'org' will be in small letter
print(os.path.join(root,file))
filelist.append(os.path.join(root,file))
return filelist