Python 为什么format(str(var))为我的操作系统提供了一个属性错误

Python 为什么format(str(var))为我的操作系统提供了一个属性错误,python,macos,attributeerror,os.system,Python,Macos,Attributeerror,Os.system,我正在尝试制作一个Python脚本,将我连接到一个VPN服务器“number”(以这个数字作为变量) 我写道: import os VPNServer = 0 VPNServer += 1 os.system("networksetup -connectpppoeservice 'VPNServer {servernumber}'").format(servernumber = str(VPNServer)) print("→ Successfully connected to", "VPNSe

我正在尝试制作一个Python脚本,将我连接到一个VPN服务器“number”(以这个数字作为变量)

我写道:

import os
VPNServer = 0
VPNServer += 1
os.system("networksetup -connectpppoeservice 'VPNServer {servernumber}'").format(servernumber = str(VPNServer))
print("→ Successfully connected to", "VPNServer", VPNServer)
但每次我尝试运行它时,控制台都会给我一个属性错误

AttributeError: 'int' object has no attribute 'format'
我不明白,因为我接受了变量的字符串版本

如果有人能帮忙,那就太酷了


我在macOS上,使用的是Python 3.8.1

在您提供的代码片段中,您可以编写

os.system('<some string>').format(args)
由于
int
对象没有属性
format
,因此您将获得所描述的
AttributeError

你想写的是

os.system('<some string>'.format(args))

请注意,
str(VPNServer)
调用是多余的,因为
format
将自动调用所提供对象的
\uuu str\uuu
方法

os.system('<some string>').format(args)
由于
int
对象没有属性
format
,因此您将获得所描述的
AttributeError

你想写的是

os.system('<some string>'.format(args))
请注意,
str(VPNServer)
调用是多余的,因为
format
将自动调用所提供对象的
\uuu str\uu
方法