带有子进程返回的 Python 脚本必须是 str,而不是 int evrything linter 说是这种情况

标签 python python-3.x shell kvm

<分区>

当我运行以下代码时出现以下错误。

版本信息:

Python 3.6.5 (default, May 11 2018, 04:00:52) [GCC 8.1.0] on linux

代码:

正确格式位于 https://gist.github.com/Drunkenpanda2000/31f76521ce1166b804a539f40ec21c60

#!/usr/bin/env python

import subprocess

#will be replaced with inputs from Chef

name='test' 
vcpus=1 
memory=2048 
iso='/var/lib/libvirt/images/Centos.iso' 
discsize= 80 
os_type='linux' 
os_variant='centos7' 
network_bridge='default'

#setting up the command

args = (
    'virt-install' + 
    ' --name=' + name + 
    ' --vcpus=' + vcpus + 
    ' --memory=' + memory + 
    ' --cdrom=' + iso + 
    ' --disk size=' + discsize + 
    ' --os-type=' + os_type + 
    ' --os-varient=' + os_variant +
    ' --network bridge=' + network_bridge +
    " --extra-args 'console=ttyS0,115200n8 serial'" )

#execute the commands in bash

subprocess.call(args, shell=True)

错误

[drunkenpanda@Diablo Scripts]$ ./createvm.py  Traceback (most recent call last):   File "./createvm.py", line 27, in <module>
    ' --network bridge=' + network_bridge + TypeError: must be str, not int

新代码

    args = ['virt-install',
    ' --name',name,
    ' --vcpus',str(vcpus),
    ' --memory',str(memory),
    ' --cdrom',iso,
    ' --disk-size',str(discsize),
    ' --os-variant',os_variant,
    ' --os-type',os_type,
    ' --network bridge',network_bridge]


    # " --extra-args 'console=ttyS0,115200n8 serial'"\


#execute the commands in bash

subprocess.call(args, shell=False)

新错误

./createvmattend.1.py 
usage: virt-install --name NAME --memory MB STORAGE INSTALL [options]
virt-install: error: unrecognized arguments:  --name bob  --vcpus 1  --memory 2048  --cdrom /var/lib/libvirt/images/Centos.iso  --disk-size 80  --os-variant centos7.0  --os-type linux  --network bridge virbr0

最佳答案

您只能用字符串连接字符串,不能连接整数。

粗糙但应该有效:

args = (
    'virt-install' + 
    ' --name=' + name + 
    ' --vcpus=' + str(vcpus) +     # fix
    ' --memory=' + str(memory) +    # fix
    ' --cdrom=' + iso + 
    ' --disk size=' + str(discsize) +  # fix
    ' --os-type=' + os_type + 
    ' --os-varient=' + os_variant +
    ' --network bridge=' + network_bridge +
    " --extra-args 'console=ttyS0,115200n8 serial'" )

如果您使用的是 python 3.6,您可能希望切换到文字字符串插值 PEP-498 :

someValue = 22
c = f"This text contains {someValue}"

或者您可以使用 .format()

someValue = 22
c = "This text contains {}".format(someValue) # positional replacement of {} by var

关于带有子进程返回的 Python 脚本必须是 str,而不是 int evrything linter 说是这种情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50708912/

相关文章:

python - 从另一个列表中查找数字索引

shell - 在 Debian 中的 bash 中创建存档文件,无需安装软件

git - 如何使用初始化 shell 脚本克隆私有(private) git 存储库

python - 有没有更 pythonic 的方法来为一个类设置多个默认参数?

python - 属性错误: 'tuple' object has no attribute for the code below

python - 选择至少一列中的值为负数的行

linux - 将 shell 脚本添加到配置文件

python - 如何在 Python 中设置 Delaunay 三角形边的最大距离

python - 如何初始化空列表?

python - 如何根据输入的单词给出评分