简体   繁体   English

如何重复将参数传递给python文件

[英]How to repeatedly pass arguments to a python file

I have a python script which is run as follows: 我有一个python脚本,运行如下:

python script.py -n name 

Also, I have a file (or say list) which contains all the name values such as follows: 另外,我有一个文件(或说列表),其中包含所有name值,如下所示:

name1
name2
name3
name4
...
...
name1000

So, I want to run the python script to pass all these names as the arguments. 所以,我想运行python脚本来传递所有这些名称作为参数。 The dumbest and laziest way of doing this task is that I created a shell script (I wrote a python script to generate this content) say run.sh as follows: 执行此任务的最愚蠢和最懒惰的方式是我创建了一个shell脚本(我写了一个python脚本来生成这个内容)说run.sh如下:

python script.py -n name1
python script.py -n name2
python script.py -n name3
python script.py -n name4
........
........
python script.py -n name1000

and I run this shell script as sh run.sh . 我将此shell脚本作为sh run.sh

I am sure that there must be a smarter/elegant way of doing this. 我确信必须有一个更聪明/更优雅的方式来做到这一点。 Any tips? 有小费吗?

Also, is it possible to free (clear) the python memory after each execution 此外,是否可以在每次执行后释放(清除)python内存

I would suggest passing a file location (eg 'names.txt' ) as the parameter to your python script when called from shell. 我建议从shell调用时将文件位置(例如'names.txt' )作为参数传递给python脚本。 Then within the python script, import the names and work with them one by one. 然后在python脚本中,导入名称并逐个使用它们。

To your second question, if you wrap the logic of script.py in a function (eg called script_function ) which takes param name , and call script_function(name) for each name in 'names.txt' , you should keep your memory use down. 对于第二个问题,如果将script.py的逻辑包装在一个函数(例如,名为script_function )中,该函数采用参数name ,并为'names.txt'每个name调用script_function(name) ,则应该保持内存使用率。 The reason is that all the variables created in script_function would be local to that function call and deleted/replaced on the next function call for working with the next name. 原因是在script_function创建的所有变量都是该函数调用的本地变量,并在下一个函数调用中删除/替换以使用下一个名称。

另一种选择是awk内置的系统功能,所以:

awk '{ system("python script.py -n "$0) }' filename

try: 尝试:

cat <file_with_list_of_names_1_per_line> | xargs python script.py -n

that should do it, if not ... try wrapping "python script.py -n" in a bash script. 应该这样做,如果不是...尝试在bash脚本中包装“python script.py -n”。 Something simple, let say a script named "call_my_script.sh" which would contain: 简单的说,假设一个名为“call_my_script.sh”的脚本包含:

#!/bin/bash
python script.py -n $1

, then you could call it with: ,然后你可以用:

cat <file_with_list_of_names_1_per_line> | xargs call_my_script.sh
# this will call/execute "call_my_script.sh name", for each name in the file, 1 at a time

You could also use the "-P" option of "xargs", to run several instances in parallel (watch out for concurențial access to resources, like writing to same output file or similar, could produce strange results) 您还可以使用“xargs”的“-P”选项,并行运行多个实例(注意对资源的简要访问,比如写入相同的输出文件或类似文件,可能会产生奇怪的结果)

.. | xargs -P <n> ...

to run "n" instances of the script in parallel 并行运行脚本的“n”个实例

Side note: also an important aspect for whom is not familiar with "xargs", it will treat each word individually, meaning if on a line there would've been 2 (or more) words "word1 word2" ... that would make it call "the script" 2 (or more) times, 1 for each word. 旁注:也是一个不熟悉“xargs”的重要方面,它会单独处理每个单词 ,这意味着如果在一条线上会有2个(或更多)单词“word1 word2”......那会使它称“脚本”为2(或更多)次,每个单词为1。 It might not be the expected behavior, so it worth mentioning. 它可能不是预期的行为,所以值得一提。

Considering you have a list of names you can do the following in shell script 考虑到您有一个名称列表,您可以在shell脚本中执行以下操作

list="name1 name2 name 3 ... name1000"
for i in $list; do
python script.py -n $i
done

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

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