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).
$ 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
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'])
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
(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