Home  >  Article  >  Backend Development  >  How to Recursively Read Folder Contents Beyond One Subdirectory Level in Python?

How to Recursively Read Folder Contents Beyond One Subdirectory Level in Python?

Barbara Streisand
Barbara StreisandOriginal
2024-10-18 14:42:03716browse

How to Recursively Read Folder Contents Beyond One Subdirectory Level in Python?

Python Recursive Folder Read

This question arises when working with a Python script to recursively read the contents of text files in a folder structure. However, the initial code provided encountered a limitation of reading only one folder deep.

Problem Identification

The issue lies in the hardcoded path: outfileName = rootdir "/" folder "/py-outfile.txt". This path assumes that the target file is located one subfolder below the root directory.

Solution

To address this limitation, we need to understand the return values of os.walk:

  • root: The current path being processed.
  • subdirs: A list of subdirectories in root.
  • files: A list of non-directory files in root.

Instead of using filePath = rootdir '/' file, we should utilize os.path.join to combine root and file: filePath = os.path.join(root, file). This approach allows us to correctly navigate through the folder hierarchy.

Revised Code

Here's a revised version of the code:

import os
import sys

walk_dir = sys.argv[1]

print('walk_dir = ' + walk_dir)

# Convert to absolute path (recommended if the working directory may change during execution)
walk_dir = os.path.abspath(walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))

for root, subdirs, files in os.walk(walk_dir):
    print('--\nroot = ' + root)
    list_file_path = os.path.join(root, 'my-directory-list.txt')
    print('list_file_path = ' + list_file_path)

    with open(list_file_path, 'wb') as list_file:
        for subdir in subdirs:
            print('\t- subdirectory ' + subdir)

        for filename in files:
            file_path = os.path.join(root, filename)
            print('\t- file %s (full path: %s)' % (filename, file_path))

            with open(file_path, 'rb') as f:
                f_content = f.read()
                list_file.write(('The file %s contains:\n' % filename).encode('utf-8'))
                list_file.write(f_content)
                list_file.write(b'\n')

This revised code will now recursively traverse the entire folder structure and write the contents of each file to the specified list_file_path.

The above is the detailed content of How to Recursively Read Folder Contents Beyond One Subdirectory Level in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn