尝试用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 进行这种字符串替换非常容易;您可能会考虑这样做,而不是掏空。
关于python TypeError 格式字符串的参数不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11397571/