Python: Open() using a variable

Posted by nuustik on Stack Overflow See other posts from Stack Overflow or by nuustik
Published on 2010-05-07T12:09:31Z Indexed on 2010/05/07 12:18 UTC
Read the original article Hit count: 113

Good day,

I've ran into a problem with opening a file with randomly generated name in Python 2.6.

import random

random = random.randint(1,10)

localfile = file("%s","wb") % random

Then I get an error message about the last line

TypeError: unsupported operand type(s) for %: 'file' and 'int' 

I just couldn't figure this out by myself nor with Google, but there has to be a cure for this, I believe.

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about string-formatting