Can I force os.walk to visit directories in alphabetical order?
Yes. You sort dirs in the loop. def main_work_subdirs(gl): for root, dirs, files in os.walk(gl[‘pwd’]): dirs.sort() if root == gl[‘pwd’]: for d2i in dirs: print(d2i)
Yes. You sort dirs in the loop. def main_work_subdirs(gl): for root, dirs, files in os.walk(gl[‘pwd’]): dirs.sort() if root == gl[‘pwd’]: for d2i in dirs: print(d2i)
os.walk gives you the path to the directory as the first value in the loop, just use os.path.join() to create full filename: shpfiles = [] for dirpath, subdirs, files in os.walk(path): for x in files: if x.endswith(“.shp”): shpfiles.append(os.path.join(dirpath, x)) I renamed path in the loop to dirpath to not conflict with the path variable you … Read more
I made a research on a small cache of web pages in 1000 dirs. The task was to count a total number of files in dirs. The output is: os.listdir: 0.7268s, 1326786 files found os.walk: 3.6592s, 1326787 files found glob.glob: 2.0133s, 1326786 files found As you see, os.listdir is quickest of three. And glog.glob is … Read more
Add a break after the filenames for loop: for root, dirs, filenames in os.walk(workdir): for fileName in filenames: print (fileName) break #prevent descending into subfolders This works because (by default) os.walk first lists the files in the requested folder and then goes into subfolders.
This solution uses fnmatch.translate to convert glob patterns to regular expressions (it assumes the includes only is used for files): import fnmatch import os import os.path import re includes = [‘*.doc’, ‘*.odt’] # for files only excludes = [‘/home/paulo-freitas/Documents’] # for dirs and files # transform glob patterns to regular expressions includes = r’|’.join([fnmatch.translate(x) for … Read more
No, there is no option to os.walk() that’ll skip those. You’ll need to do so yourself (which is easy enough): for root, dirs, files in os.walk(path): files = [f for f in files if not f[0] == ‘.’] dirs[:] = [d for d in dirs if not d[0] == ‘.’] # use files and dirs … Read more
os.walk uses os.listdir. Here is the docstring for os.listdir: listdir(path) -> list_of_strings Return a list containing the names of the entries in the directory. path: path of directory to list The list is in arbitrary order. It does not include the special entries ‘.’ and ‘..’ even if they are present in the directory. (my … Read more
Don’t use os.walk. Example: import os root = “C:\\” for item in os.listdir(root): if os.path.isfile(os.path.join(root, item)): print item
This will give you the desired result #!/usr/bin/python import os # traverse root directory, and list directories as dirs and files as files for root, dirs, files in os.walk(“.”): path = root.split(os.sep) print((len(path) – 1) * ‘—‘, os.path.basename(root)) for file in files: print(len(path) * ‘—‘, file)
You should be using the dirpath which you call root. The dirnames are supplied so you can prune it if there are folders that you don’t wish os.walk to recurse into. import os result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(PATH) for f in filenames if os.path.splitext(f)[1] == ‘.txt’] Edit: After the latest … Read more