Python: Not all of arguments converted during string formatting

风流意气都作罢 提交于 2020-01-22 13:23:06

问题


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()

回答1:


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 any formatting arguments, like you did with "%s.sql", but it's not legal to pass arguments but not the right amount ("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)




回答2:


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


来源:https://stackoverflow.com/questions/3089038/python-not-all-of-arguments-converted-during-string-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!