Skip to main content
 首页 » 编程设计

python TypeError 格式字符串的参数不足

2025年05月04日22yxwkf

尝试用python结构更新文件

run('sed -i s/jahil/%s/g /etc/nginx/sites-enabled/%s' % (account)) 

或

run('sed -i "s/jahil/%s/g" /etc/nginx/sites-enabled/%s' % (account)) 

错误:

TypeError: not enough arguments for format string 

有什么想法吗?非常感谢!

请您参考如下方法:

错误告诉您问题所在:您的字符串中有两个 %s 占位符,但只有一个值。我假设您想在两个地方使用相同的值。在这种情况下:

run('sed -i s/jahil/%s/g /etc/nginx/sites-enabled/%s' % (account, account)) 

或者,等效地,使用字典,以便我们可以使用命名占位符:

run('sed -i s/jahil/%(a)s/g /etc/nginx/sites-enabled/%(a)s' % {"a": account}) 

完全用 Python 进行这种字符串替换非常容易;您可能会考虑这样做,而不是掏空。