Environnement d'exploitation
Xeon E5-2620 v4 (8 noyaux) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 et ses-devel
mpich.x86_64 3.1-5.el6 et ses-devel
gcc version 4.4.7 (Et gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Utilisez 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 La fonction ʻos.walk () `qui m'intéressait en la regardant.
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.
Je l'ai essayé (sur 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)
résultat
$ 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 Il a été implémenté comme suit en référence à l'exemple de code de.
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)
résultat
$ 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
(Ajout 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)
Le traitement supplémentaire a été organisé et soigné.
Recommended Posts