简体   繁体   English

子流程函数python:在自动化中使用

[英]Subprocess function python: Use in automation

I am trying to run this command in the terminal using Python: 我正在尝试使用Python在终端中运行以下命令:

./Pascal --set=settings/1_settings.txt --runpathway=on --genescoring=sum --pval=1_snp_values.txt.gz ./Pascal --set = settings / 1_settings.txt --runpathway = on --genescoring = sum --pval = 1_snp_values.txt.gz

I need to run this script 180 times using a different pval everytime. 我需要每次使用不同的pval运行此脚本180次。 Hence, automating it via Python saves me a lot of time. 因此,通过Python自动化可以节省很多时间。

Currently I have a Python subprocess like this: 目前,我有一个Python子流程,如下所示:

subprocess.call("./Pascal --set=settings/1_settings.txt --runpathway=on --genescoring=sum --pval=1_snp_values.txt.gz") subprocess.call(“ ./ Pascal --set = settings / 1_settings.txt --runpathway = on --genescoring = sum --pval = 1_snp_values.txt.gz”)

Although, I am getting this error: 虽然,我收到此错误:

Traceback (most recent call last):
  File "test_automation.py", line 4, in <module>
    subprocess.call("./Pascal --set=settings/1_settings.txt --runpathway=on --genescoring=sum --pval=1_snp_values.txt.gz")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception

The problem is when I execute the exact same command in terminal (outside of the Python code) it works fine. 问题是,当我在终端(Python代码之外)中执行完全相同的命令时,它工作正常。 Am I using the syntax incorrectly? 我使用的语法不正确吗?

Using os.system ... 使用os.system ...

You could try using this to run your terminal command like so: 您可以尝试使用此命令来运行终端命令,如下所示:

import os
os.system("./Pascal --set=settings/1_settings.txt --runpathway=on --genescoring=sum --pval=1_snp_values.txt.gz")

And if this works, it is simple to execute in a for-loop to get it to run 180 times: 如果可行,则可以在for-loop执行,使其运行180次很简单:

import os
for _ in range(180):
    os.system("./Pascal --set=settings/1_settings.txt --runpathway=on --genescoring=sum --pval=1_snp_values.txt.gz")

Additionally, if Pascal is in the same directory that you are running your script then the path of ./ is not necessary as by default, it will search the current working directory. 另外,如果Pascal与您运行脚本的目录位于同一目录中,则默认情况下不需要./路径,它将搜索当前工作目录。 So you can change it to just Pascal --set-settings... 因此,您可以将其更改为仅Pascal --set-settings...

Using subprocess.call ... 使用subprocess.call ...

Personally, I think using os.system is a cleaner solution, however you can use subprocess.call to do the same thing in one of two ways, either: 我个人认为,使用os.system是一个清晰的解决方案,但是你可以使用subprocess.call做同样的事情在以下两种方式之一,无论是:

call Pascal and the other arguments separately from a list like: list分别调用Pascal和其他参数:

import subprocess as s
s.call(['./Pascal', '--set=settings/1_settings.txt', '--runpathway=on', '--genescoring=sum', '--pval=1_snp_values.txt.gz'])

or just set shell to true and pass in the full line as a string like: 或只是将shell设置为true并以如下形式将整个行作为字符串传递:

import subprocess as s
s.call('./Pascal --set=settings/1_settings.txt --runpathway=on --genescoring=sum --pval=1_snp_values.txt.gz', shell=True)

Hope this works for you! 希望这对您有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM