I’m trying to create a file with a unique file name for every time my script runs. I am only intending to do this to every week or month. so I chose to use the date for the file name.
JavaScript
x
2
1
f = open('%s.csv', 'wb') %name
2
is where I’m getting this error.
JavaScript
1
5
1
Traceback (most recent call last):
2
File "C:UsersUserworkspacenew3stjohnsinvoicesBabblevoiceInvoiceswpath.py", line 143, in <module>
3
f = open('%s.csv', 'ab') %name
4
TypeError: unsupported operand type(s) for %: 'file' and 'str'
5
it works if I use a static filename, is there an issue with the open function, that means you can’t pass a string like this?
name is a string and has values such as :
JavaScript
1
2
1
31/1/2013BVI
2
Many thanks for any help.
Advertisement
Answer
You need to put % name
straight after the string:
JavaScript
1
2
1
f = open('%s.csv' % name, 'wb')
2
The reason your code doesn’t work is because you are trying to %
a file, which isn’t string formatting, and is also invalid.