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

问题描述 投票:1回答:1

我正在尝试制作一个将我连接到VPN Server'编号'(此编号作为变量)的Python脚本

我写道:

import os
VPNServer = 0
VPNServer += 1
os.system("networksetup -connectpppoeservice 'VPNServer {servernumber}'").format(servernumber = str(VPNServer))
print("→ Successfully connected to", "VPNServer", VPNServer)

但是每次我尝试运行它时,控制台都会给我一个AttributeError

AttributeError: 'int' object has no attribute 'format'

我不理解,因为我使用了变量的字符串版本

[如果有人可以帮助,那将非常酷

我在macOS上,正在使用Python 3.8.1

python macos attributeerror os.system
1个回答
2
投票

在您提供的代码段中,您写

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

您正在调用format的返回值,该返回值恰好是整数。这等同于写,例如

os.system

由于os.system对象没有属性5.format(args) ,所以您获得了您描述的int

您要写的是

format

在这种情况下,您的摘要应该重新显示

AttributeError
© www.soinside.com 2019 - 2024. All rights reserved.