0

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'

1 Answer 1

2

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')]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.