Running os.walk in a non-standard way

I am trying to do the following, in the following order:

Use os.walk() to os.walk() through each directory.
Each directory has subfolders , but I'm only interested in the first subfolder . Therefore, the directory looks like this:

 /home/RawData/SubFolder1/SubFolder2 

For example. I want RawData2 to have folders that stop at SubFolder1 level.

The fact is that os.walk() goes through EVERYTHING from the RawData folder, and I'm not sure how to stop it.

Below is what I have so far. I tried a number of other combinations of dirs variable substitution for root or files, but that doesn't seem to give me what I want.

 import os for root, dirs, files in os.walk("/home/RawData"): os.chdir("/home/RawData2/") make_path("/home/RawData2/"+str(dirs)) 
+7
python
source share
2 answers

I suggest you use glob instead.

As the help on glob says:

 glob(pathname) Return a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch. However, unlike fnmatch, filenames starting with a dot are special cases that are not matched by '*' and '?' patterns. 

So your template is every first level directory, which I think would be something like this:

 /root_path/*/sub_folder1/sub_folder2 

So, you start from your root, get everything on this first level, and then look for sub_folder1/sub_folder2 . I think it works.

Combine all of this:

 from glob import glob dirs = glob('/root_path/*/sub_folder1/sub_folder2') # Then iterate for each path for i in dirs: print(i) 
+1
source share

Beware . The documentation for os.walk says:

Do not change the current working directory between walk () repeats. walk () never changes the current directory and assumes that its caller does not matter

therefore, you should avoid os.chdir("/home/RawData2/") in the walk loop.

You can easily topdown=True walk not to overwrite with topdown=True and clear dirs :

 for root, dirs, files in os.walk("/home/RawData", True): for rep in dirs: make_path(os.join("/home/RawData2/", rep ) # add processing here del dirs[] # tell walk not to recurse in any sub directory 
+1
source share

All Articles