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

带有子进程返回的Python脚本必须是str,而不是int-evrything-linter说的情况就是这样,python,python-3.x,shell,kvm,Python,Python 3.x,Shell,Kvm,当我运行以下代码时,出现以下错误 版本信息: linux上的Python 3.6.5(默认值,2018年5月11日04:00:52)[GCC 8.1.0] 代码: 正确格式在 错误 新错误 ./createvmattend.1.py usage: virt-install --name NAME --memory MB STORAGE INSTALL [options] virt-install: error: unrecognized arguments: --name bob --vc

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

版本信息:

linux上的Python 3.6.5(默认值,2018年5月11日04:00:52)[GCC 8.1.0]

代码:

正确格式在

错误

新错误

./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,您可能希望切换到文字字符串插值:

或者你可以使用


可能需要
--os variant
而不是
--os variant
。连接字符串时,不能混入整数。用stringyfied版本替换任何整数:
str(vcpus)
str(内存)
str(discsize)
。如果您使用的是3.6,请查看文字字符串插值;如果尚未使用3.6,请查看文字字符串插值,以获得更好的字符串格式。@Ry-没有查找重复,我的错。@PatrickArtner:没问题,特定于问题的答案通常也很有帮助(尽管正确的问题特定答案可能是避免使用
shell=True
,@NicTanghe–您可以传递参数列表)在命令行的特定情况下与
subprocess.call()一起使用
,正确的答案是上述任何一项都不是:与其构建一个长字符串,不如为每个参数传递一个字符串列表,这样就可以避免
shell=True
,这是一个安全漏洞。谢谢你,我现在就测试它。不过我还是有点困惑,因为它与网桥线相连,特别是它不包含一串
    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'" )
someValue = 22
c = f"This text contains {someValue}"
someValue = 22
c = "This text contains {}".format(someValue) # positional replacement of {} by var