views:

108

answers:

2

Im wrtiting a script which saves the current date and time as a filename but I get an error stating "TypeError: not all arguments converted during string formatting" I am new to Python andmay of missed something obvious. Code below:

from subprocess import Popen
import datetime

today = datetime.date.today()

today = str(today)

print today

f = open("%s.sql", "w" % (today))
x =  Popen(["mysqldump", "-u", "root", "-pucsdrv", "normalisationtion"], stdout = f)
x.wait()
f.close()
+3  A: 

You're putting the string formatting in the wrong place; it needs to be right after the string that's being formatted:

f = open("%s.sql" % (today), "w")

It's legal to not pass enough formatting arguments, like you did with "%s.sql" (passing it none), it just leaves the rest unconverted, but it's not legal to pass too many ("w" % (today) passes one, but there's no string formatting in "w"), so you get an error that not all of the arguments were used

Michael Mrozek
+2  A: 
f = open("%s.sql" % today, "w")
gilesc