Python从目录获取所有文件时出错
我有一个包含22个.sgm格式文件的文件夹。我想读取所有文件。我将传递的命令行参数的输入如下Python从目录获取所有文件时出错,python,file,Python,File,我有一个包含22个.sgm格式文件的文件夹。我想读取所有文件。我将传递的命令行参数的输入如下 /usr/local/pub/tmh/Reuters/*.sgm 因此,这应该读取文件夹中的所有sgm格式文件 我编写的使用python 3.4.2的代码是 allfiles=(glob.glob(sys.argv[1])) print(allfiles) 现在我只得到一个文件/usr/local/pub/tmh/Reuters/reut2-000.sgm。其余的没有被地球仪接收。我还试图打印参数,它
/usr/local/pub/tmh/Reuters/*.sgm
因此,这应该读取文件夹中的所有sgm格式文件
我编写的使用python 3.4.2的代码是
allfiles=(glob.glob(sys.argv[1]))
print(allfiles)
现在我只得到一个文件/usr/local/pub/tmh/Reuters/reut2-000.sgm
。其余的没有被地球仪接收。我还试图打印参数,它应该打印这个/usr/local/pub/tmh/Reuters/*.sgm
,但它打印/usr/local/pub/tmh/Reuters/reut2-000.sgm
(这是文件夹中的第一个文件)您的shell正在为您扩展glob,因此您实际上是将所有文件作为单独的参数传递给脚本,然后看看第一个
因此,shell不是将/usr/local/pub/tmh/Reuters/*.sgm
传递给脚本,而是将/usr/local/pub/tmh/Reuters/reut2-000.sgm/usr/local/pub/tmh/Reuters/reut2-001.sgm/usr/local/pub/tmh/Reuters/reut2-002.sgm
等传递给脚本
引用glob或参数,或者只从sys.argv[1://code>读取所有文件
引用论点:
python yourscript.py "/usr/local/pub/tmh/Reuters/*.sgm"
或者将glob扩展留给shell:
allfiles = sys.argv[1:]
print(allfiles)
shell正在为您扩展glob,因此您实际上是将所有文件作为单独的参数传递给脚本,然后只查看第一个参数
因此,shell不是将/usr/local/pub/tmh/Reuters/*.sgm
传递给脚本,而是将/usr/local/pub/tmh/Reuters/reut2-000.sgm/usr/local/pub/tmh/Reuters/reut2-001.sgm/usr/local/pub/tmh/Reuters/reut2-002.sgm
等传递给脚本
引用glob或参数,或者只从sys.argv[1://code>读取所有文件
引用论点:
python yourscript.py "/usr/local/pub/tmh/Reuters/*.sgm"
或者将glob扩展留给shell:
allfiles = sys.argv[1:]
print(allfiles)