简体   繁体   中英

check a directory and its subdirectory and print the path

I want to write some code to check if a directory (and its subdirectories) matches with a given string, let the string be New . And then I want to print the path from root to the specific directory that contains this string New . However, my code only prints the directories on the first level correctly, eg, C:\\\\New Folder , if there is a subdirectory inside that one matches the string as well, for example C:\\\\New Folder\\\\New Folder , it will only print as C:\\\\New Folder . For more details, please take a look at my code snipet:

 import os
 rootDir = 'C:\\New Folder'
 os.chdir(rootDir)
 for root, subdirs, files in os.walk(rootDir):
     for dirs in subdirs:
         splitdirs = dirs.split(" ")
         prefix = "New"
         if splitdirs[0] == prefix:
            newDir = os.path.join(rootDir, dirs)
            print(newDir)

so what I want is to print out C:\\\\New Folder and C:\\\\New Folder\\\\New Folder , instead it gives me C:\\\\New Folder twice. Can anyone explain why this happens? I am new so probably I'm in lack of some programming way of thinking here. Thank you for checking.

I think the only thing you have to change is following line:

newDir = os.path.join(rootDir, dirs)

to:

newDir = os.path.join(root, dirs)

Edit

If your searching word already exists in your rootDir, then you can simplify your code as follow:

import os
root_dir = 'C:\\New Folder'
searching_word= "New"
for root, subdirs, files in os.walk(root_dir):
    current_directory = os.path.basename(root)
    if searching_word in current_directory:
        print(root)

Don't look in subdirs , os.walk() works recursively, so you can just use the root . You also don't have to chdir there.

import os
root_dir = 'C:\\New Folder'
prefix = "New"
for root, subdirs, files in os.walk(root_dir):
    current_dir = root.split("\\")[-1] #name of current dir
    dirname_splitted = current_dir.split() #split() implies split(" ")
    if dirname_splitted[0] == prefix:
        print(root)

Don't know if it works on Windows, but worked on my Ubuntu with root_dir = "/tmp" and root.split("/")

EDIT: In comments, you say it should just contain the given prefix, not start with it. The code gets a bit simpler then:

import os
root_dir = 'C:\\New Folder'
prefix = "New"
for root, subdirs, files in os.walk(root_dir):
    current_dir = root.split("\\")[-1] #name of current dir
    if prefix in current_dir:
        print(root)

When you define newDir you are using your rootDir, instead of root. rootDir is your first folder, while root is the path of the current os.walk iteration.

Your code shoud be:

import os
rootDir = os.path.join(os.getcwd())
os.chdir(rootDir)
for root, subdirs, files in os.walk(rootDir):
    for dirs in subdirs:
        splitdirs = dirs.split(" ")
        prefix = "New"
        if splitdirs[0] == prefix:
            newDir = os.path.join(root, dirs)
            print(newDir)

You need this

import os
rootDir = 'D:\\New folder'
os.chdir(rootDir)
for root, subdirs, files in os.walk(rootDir):
    for dirs in subdirs:
        splitdirs = dirs.split(" ")
        prefix = "New"
        if splitdirs[0] == prefix:
            newDir = os.path.join(root, dirs)
            print(newDir)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM