如何将带有参数的一个python脚本的argv输入传递给一个没有?

时间:2017-06-19 10:44:48

标签: python bash input parameters argv

我正在处理存储在FITS文件中的大型传感器数据结构,我使用astropy模块打开和读入这些文件。 为了进行分析,我编写了几个以不同方式处理这些数据的脚本(例如init.py) - 每个脚本创建一个包含多个子图的图像文件。

现在我想创建一个init.py filename arg2 arg3 arg4 脚本,用于初始化诸如文件名和几个参数之类的内容,以便进行分析

analysis1.py

我想传递给所有下标,例如#analysis1.py from init import filename, arg2, arg3, arg4 # import everything else necessary # do things with the data # output picture as png-file

#!/bin/bash
# initialization of parameters and data read-in
python init.py filename arg2 arg3 arg4

# analysis scripts running in parallel using the parameters of init.py
python analysis1.py &
python analysis3.py &

使用bash但没有明确地再次输入这些作为参数,如

argv

我使用这种方式是因为我通常在CPU机器上工作,并希望尽可能多地使用线程/核心来提高效率。

普通导入当然不起作用,因为import init from sys import argv print(argv) #returns ['mypath/init.py'] 没有从上次运行中获取参数

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_Files extends CI_Controller
{
    function  __construct() {
        parent::__construct();
        $this->load->model('file');

    }

    function index(){
        $data = array();
        if($this->input->post('fileSubmit') && !empty($_FILES['userFiles']['name'])){
            $filesCount = count($_FILES['userFiles']['name']);
            for($i = 0; $i < $filesCount; $i++){
                $_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
                $_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
                $_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
                $_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
                $_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];

                $uploadPath = 'uploads/files/';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] = 'gif|jpg|png';

                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if($this->upload->do_upload('userFile')){
                    $fileData = $this->upload->data();
                    $uploadData[$i]['file_name'] = $fileData['file_name'];
                    $uploadData[$i]['created'] = date("Y-m-d H:i:s");
                    $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
                }
            }

            if(!empty($uploadData)){
                //Insert file information into the database
                $insert = $this->file->insert($uploadData);
                $statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
                $this->session->set_flashdata('statusMsg',$statusMsg);
            }
        }
        //Get files data from database
        $data['files'] = $this->file->getRows();
        //Pass the files data to view
        $this->load->view('upload_files', $data);
    }
        function index2(){
        $data = array();
        if($this->input->post('fileSubmit2') && !empty($_FILES['userFiles']['name'])){
            $filesCount = count($_FILES['userFiles']['name']);
            for($i = 0; $i < $filesCount; $i++){
                $_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
                $_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
                $_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
                $_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
                $_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];

                $uploadPath = 'uploads/index2/';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] = 'gif|jpg|png';

                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if($this->upload->do_upload('userFile')){
                    $fileData = $this->upload->data();
                    $uploadData[$i]['file_name'] = $fileData['file_name'];
                    $uploadData[$i]['created'] = date("Y-m-d H:i:s");
                    $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
                    $uploadData[$i]['divid'] =2;
                }
            }

            if(!empty($uploadData)){
                //Insert file information into the database
                $insert = $this->file->insert($uploadData);
                $statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
                $this->session->set_flashdata('statusMsg',$statusMsg);
            }
        }
        //Get files data from database
        $data['files'] = $this->file->getRows2();
        //Pass the files data to view
        $this->load->view('upload_files', $data);
    }

}

我怎么能实现这个目标?

2 个答案:

答案 0 :(得分:0)

如果您使用所有输入参数调用bash脚本:

./mybash.sh filename arg2 arg3 arg4

然后你可以编写这样的bash脚本来将输入参数传递给你的所有脚本:

#!/bin/bash
# initialization of parameters and data read-in
python init.py $1 $2 $3 $4

# analysis scripts running in parallel using the parameters of init.py
python analysis1.py $1 $2 $3 $4 &
python analysis3.py $1 $2 $3 $4 &

然后,您的init.py可以访问sys.argv中的参数:

# init.py

from sys import argv

print(argv)
# prints ['mypath/init.py', <filename>, <arg2>, <arg3>, <arg4>]

# everything else

分析脚本相同:

# analysis1.py

from sys import argv

print(argv)
# prints ['mypath/analysis1.py', <filename>, <arg2>, <arg3>, <arg4>]

# everything else

修改 如果目标是让您的init.py将初始输入参数带到bash脚本,更改它们然后将这些更改的值作为参数传递给分析脚本,那么您可以将init.py脚本转储到例如JSON中格式化为stdout,以便您的分析脚本可以在启动时解析它们。

为此,请更改init.py以将初始化参数转储到stdout,例如:

# init.py

import json
from sys import argv

# assign your input parameters
init_filename = argv[1]
init_arg2 = argv[2]
init_arg3 = argv[3]
init_arg4 = argv[4]

# do some initialisation on your input parameters here

# dump your initialised parameters to stdout in json format
print(json.dumps([init_filename, init_arg2, init_arg3, init_arg4]))

然后让您的分析脚本在启动时解析JSON值:

# analysis1.py

import json
from sys import argv

print(json.loads(argv[1]))
# prints [<init_filename>, <init_arg2>, <init_arg3>, <init_arg4>]

# everything else

最后,将init.py脚本中的(JSON)输出分配给变量,并将其传递给bash文件中的分析脚本:

#!/bin/bash
# initialization of parameters and data read-in
PARAMS=$(python init.py $1 $2 $3 $4)

# analysis scripts running in parallel using the parameters dumped by init.py
python analysis1.py ''"$PARAMS"'' 
python analysis3.py ''"$PARAMS"''

你可以在没有JSON的情况下做到这一点,但是你必须担心你的文件名中的空格和特殊字符以及你转储到stdout的args。

答案 1 :(得分:0)

正如Kendas所说,你必须在某个地方坚持磁盘上的参数。

这将有效:

<强> init.py

import sys
import pickle

def get_parms():
    return pickle.load(open("init.dat","rb"))

if __name__ == "__main__":
    myparms = sys.argv[:]
    pickle.dump(myparms,open("init.dat","wb"))

<强> analysis1.py

import init
myparms = init.get_parms()
print(myparms)

如果你这样运行init.py

python init.py filename arg1 arg2 arg3

然后analysis1.py将产生此输出:

['(pathname)/init.py', 'filename', 'arg1', 'arg2', 'arg3']

您几乎肯定希望允许analysis1.py接受命令行参数作为来自init的值的覆盖。