jahil
jahil

Reputation: 426

How to fix "TypeError: unsupported operand type(s)"?

I'm trying to use var value in file.write function:

profile = open("/tmp/%s.pcf", 'w+') % uid

and I get this error:

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

Any idea what I'm doing wrong?

Upvotes: 0

Views: 324

Answers (3)

Hans Then
Hans Then

Reputation: 11322

Try this:

profile = open("/tmp/%s.pcf" % uid, 'w+') 

Upvotes: 1

synthesizerpatel
synthesizerpatel

Reputation: 28036

You need the string format inside

profile = open("/tmp/%s.pcf" % uid, 'w+')

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121256

Move the string formatting operand to the string itself:

profile = open("/tmp/%s.pcf" % uid, 'w+')

You were trying to apply it to the result of the open() call, which is a file.

Upvotes: 5

Related Questions