我试图从文件中读取一行,并将内容用作os.listdir方法的参数

f = open('test.txt', "r+")
test = f.readlines()
contentlist = []
contentlist = os.listdir(test[0])

又回来了
"WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'c:\\test\n\\*.*"

这很有意义,因为这就是在数组中编写test[0]的方式。但是如何从数组中获取“c:\ test\”版本呢?

最佳答案

要按全局模式提取文件,请使用glob模块:

import glob
contentlist = glob.glob(test[0])

关于python - os.listdir使用数组输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22088686/

10-11 02:52
查看更多