What is best way to achieve the following? Each of the elements in the list needs to be appended with a common string.
path = os.path.join(os.path.dirname(__file__), 'configs'))
files = ['%s/file1', '%s/file2'] % path
But I am getting the following error:
TypeError: unsupported operand type(s) for %: 'list' and 'str'
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Mar 19, 2015 at 12:44
ILostMySpoon
2,3992 gold badges21 silver badges26 bronze badges
1 Answer 1
You need to apply it to each format in turn:
files = ['%s/file1' % path, '%s/file2' % path]
However, you should really use os.path.join() here; then the correct platform-specific directory separator will be used, always:
files = [os.path.join(path, 'file1'), os.path.join(path, 'file2')]
If this is getting repetitive, use a list comprehension:
files = [os.path.join(path, f) for f in ('file1', 'file2')]
answered Mar 19, 2015 at 12:45
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py