我想使用python显示指定路径中存在的word文档的文件名。
这可能对您有用:
for file in os.listdir(PATH): if file.endswith(".doc") or file.endswith(".docx"): print(file)
它就像使用scandir创建自定义迭代器一样简单,传递文件扩展名(对于Word文档,您可以使用docx)。像这样:
scandir
@ sxeros方法的替代方法:)