在使用 argparse 传递参数后,如何将 运行 一个 python 脚本作为批处理作业?
How can you run a python script as a batch job after passing an argument using argparse?
我想知道是否可以在使用 argparse 后将 运行 一个 python 脚本作为一个 bash 作业?我尝试这样做,首先使用 argparse 在 python 脚本文件中传递一个参数,然后我使用命令:
bash bash1.sh
到 运行 bash 文件,该文件将 运行 python 脚本文件。这导致错误
message_script.py: error: too few arguments
此错误是由于无法识别 argparse 参数造成的。有什么方法可以使用 argparse 然后 运行 python 脚本作为批处理作业传递参数?
message_script.py的内容:
import sys
import random
import numpy as np
import argparse
from PIL import Image
import matplotlib.pyplot as plt
#Input of Data File
#Proprtion Alpha
parser = argparse.ArgumentParser()
parser.add_argument("Alpha", help = "proportion of pixels to be embedded in cover image")
args = parser.parse_args()
alpha = args.Alpha
alpha = float(alpha) #args.alpha came back as a string hence needed to be converted to float
#n is the number of pixels in cover image
n = 512*512 #since all images in BossBase testbench are 512 by 512
#Length of message to be embedded in cover image
length_msg = (alpha * n)
len_msg = int(length_msg)
#Generates a random message, a 1D vector consisting of 1s and 0s
msg = np.random.randint(2, size= len_msg)
#Save message in text format representing each bit as 0 or 1 on a separate line
msg_text_file = open("/home/ns3/PycharmProjects/untitled1/msg_file.txt", "w") #Path of Data File
msg_text_file.write("\n".join(map(lambda x: str(x), msg)))
msg_text_file .close()
bash1.sh的内容:
#!/bin/sh
python message_script.py
您需要将参数传递给 python 脚本。如果你知道参数的确切数量,你可以这样写:
#!/bin/bash
# passing two arguments
python message_script.py "" ""
或全部:
#!/bin/bash
python message_script.py "$@"
我想知道是否可以在使用 argparse 后将 运行 一个 python 脚本作为一个 bash 作业?我尝试这样做,首先使用 argparse 在 python 脚本文件中传递一个参数,然后我使用命令:
bash bash1.sh
到 运行 bash 文件,该文件将 运行 python 脚本文件。这导致错误
message_script.py: error: too few arguments
此错误是由于无法识别 argparse 参数造成的。有什么方法可以使用 argparse 然后 运行 python 脚本作为批处理作业传递参数?
message_script.py的内容:
import sys
import random
import numpy as np
import argparse
from PIL import Image
import matplotlib.pyplot as plt
#Input of Data File
#Proprtion Alpha
parser = argparse.ArgumentParser()
parser.add_argument("Alpha", help = "proportion of pixels to be embedded in cover image")
args = parser.parse_args()
alpha = args.Alpha
alpha = float(alpha) #args.alpha came back as a string hence needed to be converted to float
#n is the number of pixels in cover image
n = 512*512 #since all images in BossBase testbench are 512 by 512
#Length of message to be embedded in cover image
length_msg = (alpha * n)
len_msg = int(length_msg)
#Generates a random message, a 1D vector consisting of 1s and 0s
msg = np.random.randint(2, size= len_msg)
#Save message in text format representing each bit as 0 or 1 on a separate line
msg_text_file = open("/home/ns3/PycharmProjects/untitled1/msg_file.txt", "w") #Path of Data File
msg_text_file.write("\n".join(map(lambda x: str(x), msg)))
msg_text_file .close()
bash1.sh的内容:
#!/bin/sh
python message_script.py
您需要将参数传递给 python 脚本。如果你知道参数的确切数量,你可以这样写:
#!/bin/bash
# passing two arguments
python message_script.py "" ""
或全部:
#!/bin/bash
python message_script.py "$@"