2017-07-15 28 views
0

時跳過某個文件夾,這裏是我的代碼:使用os.walk

rootdir_path_without_slash = '/home/winpc/Downloads/Prageeth/backups/Final/node-wen-app' 
rootdir_path_with_slash= '/home/winpc/Downloads/Prageeth/backups/Final/node-wen-app/' 


dir_src = (rootdir_path_with_slash) 
for subdir, dirs, files in os.walk(rootdir_path_without_slash): 
    for file in files: 
     file_name=os.path.join(subdir, file) 
     if file_name.endswith('.html'): 
      print file_name 

下面這段代碼導航從給定的源目錄中的所有子目錄搜索的.html file.I需要的,如果節點模塊跳過文件夾found.Please幫我。

+0

只要在for循環中給出條件爲if的條件爲filename =='node_modules':pass – bigbounty

回答

2

您需要在根目錄上放置一個if條件,以避免遍歷node_modules或其任何後代。你會想:

for subdir, dirs, files in os.walk(rootdir_path_without_slash): 
    if 'node_modules' in subdir: 
     continue 
    ... # rest of your code 

此外,subdir這裏是用詞不當,第一個參數os.walk回報是根路徑。