第一次,我在这里寻求一点帮助,因为我更像是一个 ServerFault 人。
我正在用 Python 编写一些脚本,到目前为止我一直很喜欢这种语言,但我遇到了一个小问题,它使我的脚本无法工作。
这是有问题的代码行:
subprocess.call('xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd',shell=True)
我用 os.popen 尝试过同样的事情。所有变量均已正确设置。
当我在我的常规 Linux shell 中执行有问题的命令时,它工作得非常好,但是当我使用我的 Python 脚本执行它时,我得到了奇怪的错误。我什至用 print 函数替换了 subprocess.call() 以确保我使用的是命令的确切输出。
我查看了我的 shell 的环境变量,但它们几乎相同......我会发布我遇到的错误,但我不确定它是否与我的问题有关。
在 /usr/share/perl5/Config/IniFiles.pm 第 614 行使用未初始化的值 $lines[0] 替换 (s///)。在 /usr 的模式匹配 (m//) 中使用未初始化的值 $_ /share/perl5/Config/IniFiles.pm 第 628 行。
我不是 Python 专家,所以我很可能在这里遗漏了一些东西。
预先感谢您的帮助,
安托万
编辑
按照 miax 的建议,我停止使用 shell=True。相反,我查看了subprocess的 Python 文档并使用了以下代码:
cmd = 'xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd'
args = shlex.split(cmd)
subprocess.call(args)
可悲的是,它并没有改变任何东西......
编辑2
我使用了 miax 给出的提示,但仍然出现上述错误...这是我使用的代码。
cmd = ['xen-create-image', '--hostname', nom, '--memory', memory, '--partitions=/root/scripts/part.tmp', '--ip', ip, '--netmask', netmask, '--gateway', gateway, '--passwd']
subprocess.call(cmd)
这真的很奇怪......当我在常规shell中运行它时,确切的命令工作正常......