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')
Related
I have this script that hopefully moves all the files in multiple folders into a new folder. I used the os.walk and shtil.copy functions. However the script does not work.
Here is the script:
import os
import shutil
for root, dirs, filename in os.walk(r"C:\Users\edward\OneDrive\Suspensia Pictures"):
MoveFrom = r"C:\Users\edward\OneDrive\Suspensia Pictures"
MoveTo = r"C:\Users\edward\OneDrive\Pics"
shutil.copy(os.path.join(MoveFrom, filename), os.path.join(MoveTo, filename))
Here is the error I get:
TypeError: join() argument must be str, bytes, or os.PathLike object, not 'list'
import os
import shutil
from pathlib import Path
for path, subdirs, files in os.walk(r"C:\Users\edward\OneDrive\Suspensia Pictures"):
MoveFrom = r"C:\Users\edward\OneDrive\Suspensia Pictures"
MoveTo = r"C:\Users\edward\OneDrive\Pics"
for name in files:
shutil.copy(os.path.join(path, name), Path(MoveTo))
As the os.walk documentation said,
filenames is a list of the names of the non-directory files in dirpath.
which means that the filename in your code is type of list and that is not acceptable type for join().
Here's a possible way to solve it,
import os
import shutil
files: list
for root, dirs, files in os.walk(r"."):
source_path = r"."
target_path = r"../test"
for file in files:
if os.path.isfile(os.path.join(source_path)):
shutil.copy(os.path.join(source_path, file), os.path.join(target_path, file))
One thing that you should consider is that the files from the result of os.walk would be the files in each folder under the root, which is recursive. So, this script only is able to handle the files in the depth 1.
For moving all the files in each of the folder under your specific directory, this script may work.
import os
import shutil
files: list
for root, dirs, files in os.walk(r"."):
target_path = r"../test"
for file in files:
source_path = os.path.join(root, file)
shutil.copy(source_path, os.path.join(target_path, file))
I have base path (for example C:\) and i want to get all my directories and sub directories full path (not only the names).
Currently i have this:
path = 'c:\'
for root, dirs, files in os.walk(path):
for dir in dirs:
print(os.path.dirname(dir))
This one for example:
print(os.path.abspath(dir))
Return the current .py scripy folder + the current folder (dir)
Any suggestions ?
You should use os.path.join to join the sub-directory names with the path of their parent directories:
import os
for root, dirs, files in os.walk(path):
for dir in dirs:
print(os.path.abspath(os.path.join(root, dir)))
You can use os.path.abspath and os.path.join for this task:
import os
path = 'c:\'
for root, dirs, files in os.walk(path):
for dir in dirs:
print (os.path.abspath(os.path.join(root, dir)))
I am trying to use the os.walk() module to go through a number of directories and move the contents of each directory into a single "folder" (dir).
In this particular example I have hundreds of .txt files that need to be moved. I tried using shutil.move() and os.rename(), but it did not work.
import os
import shutil
current_wkd = os.getcwd()
print(current_wkd)
# make sure that these directories exist
dir_src = current_wkd
dir_dst = '.../Merged/out'
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
print(file)
#need to move files (1.txt, 2.txt, etc) to 'dir_dst'
#tried: shutil.move(file, dir_dst) = error
If there is a way to move all the contents of the directories, I would be interested in how to do that as well.
Your help is much appreciated! Thanks.
Here is the file directory and contents
current_wk == ".../Merged
In current_wkthere is:
Dir1
Dir2
Dir3..
combine.py # python script file to be executed
In each directory there are hundreds of .txtfiles.
Simple path math is required to find source files and destination files precisely.
import os
import shutil
src_dir = os.getcwd()
dst_dir = src_dir + " COMBINED"
for root, _, files in os.walk(current_cwd):
for f in files:
if f.endswith(".txt"):
full_src_path = os.path.join(src_dir, root, f)
full_dst_path = os.path.join(dst_dir, f)
os.rename(full_src_path, full_dst_path)
You have to prepare the complete path of source file, and make sure dir_dst exists.
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
shutil.move(os.path.join(root, file), dir_dst)
I can walk the directory and print just folder/directory names but I would like to exclude folder names of directories that contain other directories. For some reason I am calling that a "final node" in the tree structure but I could well be fooling myself, wouldn't be the first time. =) On reveiewing the list of other answers perhaps this is called a "leaf node" ?
import os
chosen_path = (os.getcwd())
FoldersFound =[]
for root, dirs, files in os.walk(chosen_path, topdown=True):
for name in dirs:
FoldersFound.append(name)
FoldersFound.sort()
for FolderName in FoldersFound:
print FolderName
This will print the full names of the directories that have no child directories:
for root, dirs, files in os.walk(here):
if not dirs:
print '%s is a leaf' % root
To print only the base name, replace root with os.path.basename(root)
To put them in a list use:
folders = []
for root, dirs, files in os.walk(here):
if not dirs:
folders.append(root)
Likewise to put only the basename in the list, replace root with os.path.basename(root)
This is a solution using "os.listdir":
import os
def print_leaf_dir(pathname, dirname):
dirnames = [subfolder for subfolder in os.listdir(os.path.join(pathname, dirname)) if os.path.isdir(os.path.join(pathname, dirname, subfolder))]
if(dirnames):
for subfolder in dirnames:
print_leaf_dir(os.path.join(pathname, dirname), subfolder)
else:
print(os.path.join(pathname, dirname))
if(__name__ == '__main__'):
print_leaf_dir(r'C:\TEMP', '')
I'm trying to generate a text file that has a list of all files in the current directory and all of its sub-directories with the extension ".asp". What would be the best way to do this?
You'll want to use os.walk which will make that trivial.
import os
asps = []
for root, dirs, files in os.walk(r'C:\web'):
for file in files:
if file.endswith('.asp'):
asps.append(file)
walk the tree with os.walk and filter content with glob:
import os
import glob
asps = []
for root, dirs, files in os.walk('/path/to/dir'):
asps += glob.glob(os.path.join(root, '*.asp'))
or with fnmatch.filter:
import fnmatch
for root, dirs, files in os.walk('/path/to/dir'):
asps += fnmatch.filter(files, '*.asp')