user3235542
user3235542

Reputation:

simple string manipulation in python

How can I obtain the expected result below?

import glob
infiles = glob.glob('E:\\test\\*.txt')
print infiles
['E:\\test\\a.txt', 'E:\\test\\b.txt', 'E:\\test\\c.txt', 'E:\\test\\d.txt']

Expected result is:

'E:\\test\\a.txt','E:\\test\\b.txt','E:\\test\\c.txt','E:\\test\\d.txt'

My attempt is:

infiles = ','.join(x for x in infiles)
print infiles
E:\test\a.txt,E:\test\b.txt,E:\test\c.txt,E:\test\d.txt

Upvotes: 0

Views: 50

Answers (2)

CT Zhu
CT Zhu

Reputation: 54390

Simply:

import glob
infiles = glob.glob('E:\\test\\*.txt')
print infiles.__str__()[1:-1]

print is actually print infiles.__str__() under the hood, and you only want to get rid of the [ and ]...

Upvotes: 1

user2357112
user2357112

Reputation: 282026

print ','.join(map(repr, infiles))

You're looking for an output containing the repr representations of the strings in the lists, not the literal character contents of the strings. Mapping repr over the list gets you that.

Upvotes: 3

Related Questions