简体   繁体   English

使用 os.walk() 加入子目录

[英]Joining subdirectories using os.walk()

with open('images.txt', 'w') as text_file:
    for folderName, subfolders, f in os.walk(root_folder):
        for subfolder in subfolders:
            if subfolder == 'image_02':
                left_path = os.path.join(folderName, subfolder, 'data')
                left_list = [f for f in sorted(os.listdir(left_path)) if
                             not f.startswith('.') and f.endswith('.png')]
            elif subfolder == 'image_03':
                right_path = os.path.join(folderName, subfolder, 'data')
                right_list = [f for f in sorted(os.listdir(right_path)) if
                              not f.startswith('.') and f.endswith('.png')]
    if len(left_list) != len(right_list):
        print('ERROR: directory {} does not match with {}'.format(left_path, right_path))
        continue

    for left_file, right_file in zip(left_list, right_list):
        text_file.write(
            os.path.join(left_path, left_file) + " " + os.path.join(right_path, right_file) + "\n")

I can not access the left_list and right_list to join their paths as certainly they are out of scope.我无法访问left_listright_list来加入它们的路径,因为它们肯定超出了范围。
Could anyone tell how to fix this with a better code structure?谁能告诉如何用更好的代码结构来解决这个问题?

You could initialize the lists outside of the for您可以在 for 之外初始化列表

with open('images.txt', 'w') as text_file:
    left_list = []
    right_list = []

    for folderName, subfolders, f in os.walk(root_folder):
        for subfolder in subfolders:
            if subfolder == 'image_02':
                left_path = os.path.join(folderName, subfolder, 'data')
                left_list = [f for f in sorted(os.listdir(left_path)) if
                         not f.startswith('.') and f.endswith('.png')]
            elif subfolder == 'image_03':
                right_path = os.path.join(folderName, subfolder, 'data')
                right_list = [f for f in sorted(os.listdir(right_path)) if
                          not f.startswith('.') and f.endswith('.png')]
    if len(left_list) != len(right_list):
        print('ERROR: directory {} does not match with {}'.format(left_path, right_path))
        continue

    for left_file, right_file in zip(left_list, right_list):
        text_file.write(
        os.path.join(left_path, left_file) + " " + os.path.join(right_path, right_file) + "\n")
    with open(dataset['destination'], 'w') as text_file:
        left_list = []
        right_list = []

        for folderName, subfolders, f in os.walk(root_folder):
            for subfolder in subfolders:
                if subfolder == 'image_02':
                    left_path = os.path.join(folderName, subfolder, 'data')
                    for f in sorted(os.listdir(left_path)):
                        if not f.startswith('.') and f.endswith('.png'):
                            left_list.append(f)
                elif subfolder == 'image_03':
                    right_path = os.path.join(folderName, subfolder, 'data')
                    for f in sorted(os.listdir(right_path)):
                        if not f.startswith('.') and f.endswith('.png'):
                            right_list.append(f)

        if len(left_list) != len(right_list):
            print('ERROR: directory {} does not match with {}'.format(left_path, right_path))
            continue

        for left_file, right_file in zip(left_list, right_list):
            text_file.write(
                os.path.join(left_path, left_file) + " " + os.path.join(right_path, right_file) + "\n")

This would be a better way!这将是一个更好的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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