Question

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?

Was it helpful?

Solution

To fetch files by glob patterns use the glob module:

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

OTHER TIPS

Try this out

import glob
f = open('test.txt', "r+")
test = f.readlines()
contentlist = []
contentlist = glob.glob(test[0])
print contentlist
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top