문제

I am trying to read a line from a file and use the content as an argument for the os.listdir method

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

which returns

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

which makes sense because that's how test[0] is written in the array. But how do I get the "c:\test\" version out of that array?

도움이 되었습니까?

해결책

To fetch files by glob patterns use the glob module:

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

다른 팁

Try this out

import glob
f = open('test.txt', "r+")
test = f.readlines()
contentlist = []
contentlist = glob.glob(test[0])
print contentlist
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top