【发布时间】:2018-05-16 17:59:01
【问题描述】:
我正在尝试编写一个 Python 脚本,用于在文件夹中搜索所有扩展名为 .txt 的文件。在手册中,我只看到它硬编码为glob.glob("hardcoded path")。
如何将 glob 搜索模式的目录设为变量?具体来说:用户输入。
这是我尝试过的:
import glob
input_directory = input("Please specify input folder: ")
txt_files = glob.glob(input_directory+"*.txt")
print(txt_files)
尽管提供了包含.txt 文件的正确目录,但脚本会打印一个空列表[ ]。
【问题讨论】:
-
你使用的是 windows、mac 还是 linux?
-
您可能只是在输入搜索模式中缺少反斜杠(或正斜杠)
-
你能显示
print(input_directory+"*.txt")的输出吗?
标签: python python-3.x glob