Python 为什么format(str(var))为我的操作系统提供了一个属性错误
我正在尝试制作一个Python脚本,将我连接到一个VPN服务器“number”(以这个数字作为变量) 我写道: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
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
方法