[英]Pathlib - print contents of a folder
Here is my example.这是我的例子。
Ex:前任:
I have a folder that contains another 3 folders (FoldA, FoldB, and FoldC), a.txt file, and a.png file.我有一个文件夹,其中包含另外 3 个文件夹(FoldA、FoldB 和 FoldC)、a.txt 文件和 a.png 文件。
I have the following working code which works to print the contents a folder or directory.我有以下工作代码,可用于打印文件夹或目录的内容。
import pathlib
rd = pathlib.Path("E:\\Location\\MainFolder")
for td in rd.iterdir():
print(td)
The output is: output 是:
E:\Location\MainFolder\FoldA E:\位置\主文件夹\FoldA
E:\Location\MainFolder\FoldB E:\位置\MainFolder\FoldB
E:\Location\MainFolder\FoldC E:\位置\主文件夹\FoldC
E:\Location\MainFolder\image.png E:\位置\主文件夹\image.png
E:\Location\MainFolder\text.txt E:\Location\MainFolder\text.txt
Does anyone know a quick way to only print the folders and not any other file type (.txt, .bmp, .png, etc.)?有谁知道只打印文件夹而不打印任何其他文件类型(.txt、.bmp、.png 等)的快速方法? I've tried using.is_dir but it still prints everything.
我试过 using.is_dir 但它仍然打印所有内容。
Thanks in advance!提前致谢!
probably you did if td.is_dir
with is a function, so you need to execute it like this: if td.is_dir
是 function,那么您可能会这样做,所以您需要像这样执行它:
import pathlib
rd = pathlib.Path(".")
for td in rd.iterdir():
if td.is_dir():
print(td)
Kinda common problem with pathlib at beginning:)开始时 pathlib 有点常见的问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.