python遞迴遍歷資料夾檢查檔案

2021-06-09 19:34:28 字數 1144 閱讀 8867

python遞迴遍歷資料夾檢查缺少debug_new的cpp檔案

3x版本的python使用print需要用括號列印字串,如print(file_name)

import os

check_path = 'f:\\some_dir'

debug_str = '#define new debug_new'

report_log = 'report_debug_new_check.log'

check_ext = '.cpp'

report_list =

def check_file(fp):

if check_ext not in fp:

return

f = open(fp, 'r')

try:

all_text = f.read()

if debug_str not in all_text:

print fp

finally:

f.close()

def walk_dir(dirname):

try:

sub_items = os.listdir(dirname)

except:

print 'access denied:', dirname

else:

for item in sub_items:

full_path = os.path.join(dirname, item)

if os.path.isdir(full_path):

walk_dir(full_path)

else:

check_file(full_path)

if __name__ == "__main__":

if os.path.isdir(check_path):

check_path += '\\'

walk_dir(check_path)

log_file = open(os.path.join(check_path, report_log), 'w')

try:

log_file.writelines(report_list)

finally:

log_file.close()

python 遞迴遍歷資料夾

usr bin python import os.path def readxmls folder 三個引數 分別返回1.父目錄 2.所有資料夾名字 不含路徑 3.所有檔案名字 for parent,dirnames,filenames in os.walk folder for dirname i...

使用遞迴遍歷資料夾

using system using system.data using system.configuration using system.collections using system.web using system.web.security using system.web.ui usin...

work Python 遞迴遍歷資料夾

import os def get log path dict log path home logs for root,dirs,files in os.walk log path log path dict dict for dir name in dirs dir path os.path.jo...