[英]How to zip a folder and all of its files in Python, while preserving the folder name and relative paths for its contents?
[英]How to make python spot a folder and print its files
我希望能够使python打印C驱动器中的所有内容。 我已经弄清楚了,可以在驱动器的第一“层”上打印内容,
def filespotter():
import os
path = 'C:/'
dirs = os.listdir( path )
for file in dirs:
print(file)
但我希望它进入其他文件夹并打印其他所有文件夹中的内容。
免责声明 os.walk
很好,我在这里提供一个更简单的解决方案。
如果您使用的是python 3.5或更高版本,则可以将glob.glob
与'**'
和recursive=True
例如: glob.glob(r'C:\\**', recursive=True)
请注意,获取C:\\驱动器的整个文件列表可能需要很多时间。
如果您不需要同时使用整个列表,则glob.iglob
是一个合理的选择。 除了使用迭代器而不是列表以外,用法是相同的。
在C:\\下打印所有内容
for filename in glob.iglob(r'C:\**', recursive=True):
print(filename)
它会尽快给您输出。
但是,如果您没有可用的python 3.5,则可以查看glob.py的源代码并将其调整为适合您的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.