Python:os.system的输入被拆分
我希望将列表列表作为参数传递给python程序。当我在普通的shell上做同样的事情时,它工作得非常好,但是当我在os.system中做同样的事情时,它只会分割我的列表Python:os.system的输入被拆分,python,linux,list,os.system,Python,Linux,List,Os.system,我希望将列表列表作为参数传递给python程序。当我在普通的shell上做同样的事情时,它工作得非常好,但是当我在os.system中做同样的事情时,它只会分割我的列表 import sys import json import os #path=~/Desktop/smc/fuzzy/ os.system("test -d results || mkdir results") C1=[-5,-2.5,0,2.5,5];spr1=2.5;LR1=[10,20,30,30,30] C2=[-4,-
import sys
import json
import os
#path=~/Desktop/smc/fuzzy/
os.system("test -d results || mkdir results")
C1=[-5,-2.5,0,2.5,5];spr1=2.5;LR1=[10,20,30,30,30]
C2=[-4,-3,-2,-1,0,1,2,3,4];spr2=1;LR2=[30,40,50,50,50]
C3=[-4,-2,0,2,4];spr3=2;LR3=[40,50,60,60,60]
arg=[[spr1,LR1,C1],[spr2,LR2,C2],[spr3,LR3,C3]]
for i in range(len(arg)):
print ('this is from the main automate file:',arg[i])
print('this is stringized version of the input:',str(arg[i]))
inp=str(arg[i])
os.system("python "+"~/Desktop/smc/fuzzy/"+"name_of_my_python_file.py "+ inp)
os.system("mv "+"*_"+str(arg[i])+" results")
这就是它抛出的错误-
('this is from the main automate file:', [2.5, [10, 20, 30, 30, 30], [-5, -2.5, 0, 2.5, 5]])
('this is stringized version of the input:', '[2.5, [10, 20, 30, 30, 30], [-5, -2.5, 0, 2.5, 5]]')
('from the main executable file:', ['/home/amardeep/Desktop/smc/fuzzy/name_of_my_python_file.py', '[2.5,', '[10,', '20,', '30,', '30,', '30],', '[-5,', '-2.5,', '0,', '2.5,', '5]]'])
在第三行中,它只是用逗号分割列表,从而弄乱了列表。有没有办法让我绕过这个?
而不是传递一个整洁的列表,如:
[2.5, [10, 20, 30, 30, 30], [-5, -2.5, 0, 2.5, 5]]
它正在通过类似于
[2.5,', '[10,', '20,', '30,', '30,', '30],', '[-5,', '-2.5,', '0,', '2.5,', '5]]']
我需要能够将列表列表作为参数传递给python程序
os.system
,它已被弃用,并且无法使用带引号的参数组成正确的命令行,等等。。。(因为inp
包含空格,所以需要引用,而且很快就会变得一团糟)shutil.move时,不要使用mv
我的建议是:使用
子流程。检查调用(python谢谢!让我试试这个!另外,它会正确地阅读列表,不会弄乱列表吗?我想你可以避免创建python系统调用。只需创建函数并导入它们(请参见我答案的结尾)非常感谢Jean!你的帮助真的很有价值!它为我省去了很多麻烦!但是现在我遇到了一些其他问题。我可能会为此打开另一个线程。但是感谢这一个。还有一件事,当我按照你上面的建议进行更改时,我的代码运行良好,但是它是按顺序运行的,并且只运行一次正在使用一个内核。我希望以这样的方式运行它,即我的python程序在3个不同的内核中启动,并带有这些单独的参数。谢谢,不要打开另一个线程。只需创建一个python线程
并运行子进程。检查线程中的调用
。
import subprocess
subprocess.check_call(["python",
os.path.expanduser("~/Desktop/smc/fuzzy/name_of_my_python_file.py"),inp])
import glob,shutil
for file in glob.glob("*_"+str(arg[i])):
shutil.move(file,"results")
os.system("test -d results || mkdir results")
if not os.path.isdir("results"):
os.mkdir("results")