python文件迭代器
How to iterate all files and directories under a directory in Python?
For example, I would like to iterate each file and directory under /mnt/data/
例如,我想遍历/ mnt / data /下的每个文件和目录
/mnt/data/
|-- file.txt
`-- great
That is:
那是:
/mnt/data/files.txt
/mnt/data/great
You can use the os.walk
to do this:
您可以使用os.walk
执行此操作:
root, dirs, files = os.walk(path).next()
root
will be the path.dirs
will contain the directories.files
will contain the files.
root
将是路径。
dirs
将包含目录。
files
将包含文件。
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-iterate-all-files-and-directories-under-a-directory-in-python/
python文件迭代器