在 Python 中寻找类似于 Java 的文件遍历函数
在 Java 中,您可以执行 File.listFiles() 并接收目录中的所有文件。 然后您可以轻松地通过目录树进行递归。
在 Python 中是否有类似的方法可以做到这一点?
In Java you can do File.listFiles()
and receive all of the files in a directory. You can then easily recurse through directory trees.
Is there an analogous way to do this in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
就在这里。 Python 的方式甚至更好。
有三种可能性:
1)像File.listFiles():
Python有函数os.listdir(path)。 它的工作原理类似于 Java 方法。
2) 使用 glob 进行路径名模式扩展:
模块 glob 包含使用类似 Unix shell 的模式列出文件系统上的文件的函数,例如
<代码>
3)使用walk进行文件遍历:
Python的os.walk函数非常好。
walk 方法返回一个生成函数,该函数递归列出给定起始路径下的所有目录和文件。
一个例子:
<代码>
You can even on the fly remove directories from "dirs" to avoid walking to that dir: if "joe" in dirs: dirs.remove("joe") to avoid walking into directories called "joe".
listdir 和 walk 记录在此处。
glob 记录在此处。
Yes, there is. The Python way is even better.
There are three possibilities:
1) Like File.listFiles():
Python has the function os.listdir(path). It works like the Java method.
2) pathname pattern expansion with glob:
The module glob contains functions to list files on the file system using Unix shell like pattern, e.g.
3) File Traversal with walk:
Really nice is the os.walk function of Python.
The walk method returns a generation function that recursively list all directories and files below a given starting path.
An Example:
You can even on the fly remove directories from "dirs" to avoid walking to that dir: if "joe" in dirs: dirs.remove("joe") to avoid walking into directories called "joe".
listdir and walk are documented here.
glob is documented here.
作为一个长期的 Pythonista,我不得不说 std 库中的路径/文件操作函数是低于标准的:它们不是面向对象的,它们反映了过时的,让-wrap-OS-system-functions-without-思维哲学。 我衷心推荐“path”模块作为包装器(如果您必须知道,则围绕 os、os.path、glob 和 tempfile):更好且 OOPy:http://pypi.python.org/pypi/path.py/2.2
这是带有路径模块的 walk() :
As a long-time Pythonista, I have to say the path/file manipulation functions in the std library are sub-par: they are not object-oriented and they reflect an obsolete, lets-wrap-OS-system-functions-without-thinking philosophy. I'd heartily recommend the 'path' module as a wrapper (around os, os.path, glob and tempfile if you must know): much nicer and OOPy: http://pypi.python.org/pypi/path.py/2.2
This is walk() with the path module:
尝试在 os 模块中使用“listdir()”(docs):
Try "listdir()" in the os module (docs):
直接来自 Python 参考库
Straight from Python's Refererence Library
查看 os.walk() 和示例 这里。 使用 os.walk() 可以轻松处理整个目录树。
上面链接中的示例...
Take a look at
os.walk()
and the examples here. Withos.walk()
you can easily process a whole directory tree.An example from the link above...
如果您还需要子目录,请使用 os.path.walk。
Use os.path.walk if you want subdirectories as well.
我建议不要使用 os.path.walk,因为它已在 Python 3.0 中被删除。 无论如何,
os.walk
更简单,或者至少我发现它更简单。I'd recommend against
os.path.walk
as it is being removed in Python 3.0.os.walk
is simpler, anyway, or at least I find it simpler.您还可以查看 Unipath,Python 操作系统的面向对象包装器,
os.path
和shutil
模块。示例:
通过 Cheese shop 安装:
You can also check out Unipath, an object-oriented wrapper of Python's
os
,os.path
andshutil
modules.Example:
Installation through Cheese shop:
由于我已经用Python编程很长时间了,我多次使用os模块并制作了自己的函数来打印目录中的所有文件。
该函数的代码:
Seeing as i have programmed in python for a long time, i have many times used the os module and made my own function to print all files in a directory.
The code for the function: