Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

https://docs.python.org/3/library/os.html The function ʻos.walk () `that I was curious about when I was looking at it.

os.walk(top, topdown=True, onerror=None, followlinks=False) Generate the file names in a directory tree by walking the tree either top-down or bottom-up.

I tried it (on Python 3.6.0).

Preparation

$ mkdir -p TOP/MIDDLE/BOTTOM
$ touch TOP/MIDDLE/mfile1.txt
$ touch TOP/MIDDLE/mfile2.txt
$ touch TOP/MIDDLE/BOTTOM/bfile1.txt
$ touch TOP/MIDDLE/BOTTOM/bfile2.txt
$ touch TOP/tfile1.txt

code> Display the result of os.walk ()

test_python_170327a.py


import os

res = os.walk(".")
for elem in res:
	print(elem)
	

result


$ python test_python_170327a.py 
('.', ['TOP'], ['test_python_170325b.py', 'test_python_170327a.py'])
('./TOP', ['MIDDLE'], ['tfile1.txt'])
('./TOP/MIDDLE', ['BOTTOM'], ['mfile2.txt', 'mfile1.txt'])
('./TOP/MIDDLE/BOTTOM', [], ['bfile2.txt', 'bfile1.txt'])

code> Get the path of each file

https://docs.python.org/3/library/os.html I implemented it as follows with reference to the sample code of.

test_python_170327b.py


import os

res = os.walk(".")
for elem in res:
	dirpath, dirnames, filenames = elem
	for afile in filenames:
		res = os.path.join(dirpath, afile)
		print(res)

result


$ python test_python_170327b.py 
./test_python_170327b.py
./test_python_170325b.py
./test_python_170327a.py
./TOP/tfile1.txt
./TOP/MIDDLE/mfile2.txt
./TOP/MIDDLE/mfile1.txt
./TOP/MIDDLE/BOTTOM/bfile2.txt
./TOP/MIDDLE/BOTTOM/bfile1.txt

after code> refactoring

(Addition 2017/03/28)

test_python_170328a.py


import os

for dirpath, dirnames, filenames in os.walk("."):
    for afile in filenames:
        res = os.path.join(dirpath, afile)
        print(res)

The extra processing has been organized and neat.

Recommended Posts

Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
Output the specified table of Oracle database in Python to Excel for each file
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
[Beginner memo] How to specify the library reading path in Python
Get the path to the systemd unit file
[Python] Read the specified line in the file
[Python Kivy] How to get the file path by dragging and dropping
How to use the C library in Python
How to get the files in the [Python] folder
Recursively search the specified directory to see the file
[Modint] Decoding the AtCoder Library ~ Implementation in Python ~
I want to get the path of the directory where the running file is stored.
How to read all the classes contained in * .py in the directory specified by Python
I want to get the file name, line number, and function name in Python 3.4
How to get a list of files in the same directory with python
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
How to know the current directory in Python in Blender
[Internal_math version (2)] Decoding the AtCoder Library ~ Implementation in Python ~
Try to get the function list of Python> os package
[Python] Open the csv file in the folder specified by pandas
Download the file in Python
How to debug the Python standard library in Visual Studio
Change the standard output destination to a file in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
Get the MIME type in Python and determine the file format
Developed a library to get Kindle collection list in Python
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
[Python] Get the script execution directory with an absolute path
Python> __init__.py> Required to handle the specified directory as a package (empty file is acceptable)
Get the index of each element of the confusion matrix in Python
How to know the internal structure of an object in Python
Search the file name including the specified word and extension in the directory
Hit the New Relic API in Python to get the server status
Read a file in Python with a relative path from the program
Get the formula in an excel file as a string in Python
File / folder path manipulation in Python
Save the binary file in Python
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
Get the host name in Python
Get the file path using Pathlib
How to get the date and time difference in seconds with python
Test & Debug Tips: Create a file of the specified size in Python
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
View the full path (absolute path) of a file in a directory in Linux Bash
The file name was bad in Python and I was addicted to import
Switch the module to be loaded for each execution environment in Python
I made a program to check the size of a file in Python
I created a script to check if English is entered in the specified position of the JSON file in Python.
Pin current directory to script directory in Python
Convert psd file to png in Python
How to get a stacktrace in python
What is "mahjong" in the Python library? ??
Read the file line by line in Python
Read the file line by line in Python
[Python] Get the character code of the file