使用子进程从数组调用命令?

时间:2016-05-05 22:39:02

标签: python subprocess

我有以下代码:

'''
Created by Baran Skistad, bjskistad on GitHub. 
Please give credit to Baran Skistad if you use this.
'''
index = 0
file = 'example.shs'
data = ''
loop = 0
loopString = ''
commands = ['']
command = 0
done = False
import subprocess

with open(file, 'r') as myfile:
    data = myfile.read().replace('\n', '')

commands = [part + ';' for part in data[:-1].split(';')]
print commands
if done:
    while command <= len(commands):
        ccommand = commands[command]
        print ccommand[command][3:len(ccommand[command]-1)]
        subprocess.call([ccommand[3:len(commands[command]-1)]])
        command = command + 1

它给了我以下错误:

  File "/home/ubuntu/workspace/source.py", line 23
    print ccommand[command][3:len(ccommand[command]-1)]
                 ^
SyntaxError: invalid syntax


Process exited with code: 1

example.shs文件如下所示:

run rm wha;
run git clone https://github.com/jel-lang/jel.git;

如您所见,这是我制作的自定义语言。我希望Python文件要做的是从列表中的每个项目中取出前4个字母和最后一个字母,然后使用subprocess运行它。

1 个答案:

答案 0 :(得分:0)

这是python 2代码,你使用python 3运行它。使用命令python2运行它,你就不会遇到这个问题。