File access under the directory

How to access files under the directory in a loop memo

I want to process all files in a directory that is python, A memo to forget every time.

fild_all_files.py


def fild_all_files(directory):
    for root, dirs, files in os.walk(directory):
        yield root
        for file in files:
            yield os.path.join(root, file)

Just pass the path of the target directory as an argument and loop, and the contents of the file will be returned.

Example of use

main.py


for filename in fild_all_files(file_dir):
    print filename

that's all

Recommended Posts

File access under the directory
[Linux] Directory under the root
Download the file with PHP [Under construction]
Extract only the file name excluding the directory in the directory
Access files in the same directory as the executable
Delete all pyc files under the specified directory
Recursively search the specified directory to see the file
Unzip all zip files under the current directory
[Python] File / directory operations
Download the file in Python
Recursively copy files from the directory directly under the directory using Python
Access the file with a relative path from the execution script.
Unzip the internet zip file
[Note] Import of a file in the parent directory in Python
Linux file and directory permissions
Mac Linux Check the capacity directly under a specific directory
Can you delete the file?
Set the last modified date of the child file to the modified date of the parent directory
Search the file name including the specified word and extension in the directory
Save the Pydrive authentication file in a different directory from the script
Dig the directory and create a list of directory paths + file names
[Python] Check the current directory, move the directory
Extract the xz file with python
Run all unittests under a directory
Follow the file hierarchy with fts
Simply view the Jupyter notebook file
Download the file deployed with appcfg.py
The story of the "hole" in the file
[Linux] File and directory operation commands
Extract the targz file using python
Open the file with the default app
Access the Twitter API in Python
Split the pyramid framework ini file
Get the file path using Pathlib
Command for the current directory Python
Replace the directory name and the file name in the directory together with a Linux command.
List all files under the current directory line by line with full path
View the full path (absolute path) of a file in a directory in Linux Bash