I'm trying to pass a parameter that enables verbose logging with syslog. Passing -v to the application definitely works as expected. So I am trying to add this option to a json config file.
Here is what I have in the json config:
"rtl": {
"freq": 144.390,
"ppm": -3,
"gain": 44.5,
"debug": true,
"offset_tuning": false,
"device_index": 0,
},
Here is what I have in the code. Note: All the other parts work except when I add the -v statement.
if self.config['source'] == 'rtl':
proc_src = subprocess.Popen(
['rtl_fm', '-f', str(int(self.config['rtl']['freq'] * 1e6)), '-s', '22050',
'-v', str(self.config['rtl'].get('debug', 'true')), '-v'],,
'-p', str(self.config['rtl']['ppm']), '-g', str(self.config['rtl']['gain']),
'-E', 'offset' if self.config['rtl'].get('offset_tuning', False) else 'none',
'-d', str(self.config['rtl'].get('device_index', 0)), '-'],
stdout=subprocess.PIPE, stderr=open('/dev/null')
)
Here is the error I get:
SyntaxError: ('invalid syntax', ('/usr/local/lib/python2.7/dist-packages/pymultimonaprs/multimon.py', 37, 62, "\t\t\t\t\t'-v', str(self.config['rtl'].get('debug', 'true')), '-v'],\n"))
It seems like tabs are getting thrown into the -d statement. I'm fairly new to python and just struggling with this. Any ideas?
Two commas:
'-v', str(self.config['rtl'].get('debug', 'true')), '-v'],,
^^
proc_src = subprocess.Popen(
['rtl_fm',
'-f', str(int(self.config['rtl']['freq'] * 1e6)),
'-s', '22050',
'-v', str(self.config['rtl'].get('debug', 'true')),
'-v', #?
'-p', str(self.config['rtl']['ppm']),
'-g', str(self.config['rtl']['gain']),
'-E', 'offset'
if self.config['rtl'].get('offset_tuning', False) else 'none',
'-d', str(self.config['rtl'].get('device_index', 0)), '-'
],
stdout=subprocess.PIPE, stderr=open('/dev/null'))
Related
I am trying to pass a command line arguments to AWS lambda as a event json.
I have a list out of the command line args like:
['engine', '-e', '100', '-w', '4']
When I pass this list I want to it get passed as:
'engine', '-e', '100', '-w', '4'
I am trying to use it like this:
listcommd = ['engine', '-e', '100', '-w', '4']
command = ["--bucket", "mybucket", f"{listcommand}"]
I want the end result to look like:
command = ["--bucket", "mybucket", 'engine', '-e', '100', '-w', '4']
but it is looking like:
command = ["--bucket", "mybucket", "['engine', '-e', '100', '-w', '4']"]
Lists support concatenation. Just do this:
command = ["--bucket", "mybucket"] + listcommand
argument_list = ['name=Jon', 'id=100' ]
output = subprocess.check_output(
['/usr/bin/python', 'test.py', argument_list ], stderr=subprocess.STDOUT)
In simple terms, I am trying to invoke a script using subprocess called test.py; I want to pass arguments to test.py through a list. Importatnt - List can be of any size.
Things I tried ,
output = subprocess.check_output(
['/usr/bin/python', 'test.py', ", ".join(argument_list) ], stderr=subprocess.STDOUT)
and
output = subprocess.check_output(
['/usr/bin/python', 'test.py', '%s' % argument_list ], stderr=subprocess.STDOUT)
Neither works because in subprocess.checkoutput should be (' ', ' ' ,' ') etc....
Is there a better way to do this ?
You can make a new list by adding lists together:
output = subprocess.check_output(['/usr/bin/python', 'test.py'] + argument_list, stderr=subprocess.STDOUT)
This will run test.py with argument_list as it's command-line parameters.
I want to execute a shell script with 3 arguments from a python script. (as described here: Python: executing shell script with arguments(variable), but argument is not read in shell script)
Here is my code:
subprocess.call('/root/bin/xen-limit %s %s %s' % (str(dom),str(result),str('--nosave'),), shell=True)
variables dom and result are containing strings.
And here is the output:
/bin/sh: --nosave: not found
UPDATE:
That is the variable "result":
c1 = ['/bin/cat', '/etc/xen/%s.cfg' % (str(dom))]
p1 = subprocess.Popen(c1, stdout=subprocess.PIPE)
c2 = ['grep', 'limited']
p2 = subprocess.Popen(c2, stdin=p1.stdout, stdout=subprocess.PIPE)
c3 = ['cut', '-d=', '-f2']
p3 = subprocess.Popen(c3, stdin=p2.stdout, stdout=subprocess.PIPE)
c4 = ['tr', '-d', '\"']
p4 = subprocess.Popen(c4, stdin=p3.stdout, stdout=subprocess.PIPE)
result = p4.stdout.read()
After that, the variable result is containing a number with mbit (for example 16mbit)
And dom is a string like "myserver"
from subprocess import Popen, STDOUT, PIPE
print('Executing: /root/bin/xen-limit ' + str(dom) + ' ' + str(result) + ' --nosave')
handle = Popen('/root/bin/xen-limit ' + str(dom) + ' ' + str(result) + ' --nosave', shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE)
print(handle.stdout.read())
If this doesn't work i honestly don't know what would.
This is the most basic but yet error describing way of opening a 3:d party application or script while still giving you the debug you need.
Why not you save --nosave to a variable and pass the variable in subprocess
It's simpler (and safer) to pass a list consisting of the command name and its arguments.
subprocess.call(['/root/bin/xen-limit]',
str(dom),
str(result),
str('--nosave')
])
str('--nosave') is a no-op, as '--nosave' is already a string. The same may be true for dom and result as well.
I have a python script to parse input argument from user .my code is like this
def get_arg(argv):
result = {}
input_file=stag_db=main_tb=stag_table=main_table = "";
debug="N"
msg = ''' Syntax: dt transaction date
-i input_file (E.g. input_file.tar.gz)
-ds staging_database
-dm main_database
-ts staging_table
-tm main_table
-db debug (Y/N)'''
try:
opts, args = getopt.getopt(argv,"hd:i:ds:dm:db:ts:tm:",["ifile=","ofile="])
print args
print opts
except getopt.GetoptError:
f_end_process(msg)
for opt, arg in opts:
if opt == '-h':
f_end_process(msg)
elif opt == "-i":
input_file = arg
elif opt == "-ds":
stag_db = arg
elif opt == "-dm":
main_tb = arg
elif opt == "-ts":
stag_table = arg
elif opt == "-tm":
main_table = arg
elif opt == "-db":
debug = arg
result = {'input_file':input_file,'stag_db':stag_db,'main_tb':main_tb,'stag_table':stag_table,'main_table':main_table}
print result
if '' in result.values():
exit_status=-1
f_end_process(msg)
result['debug']= debug
return result
def main():
global input_arg
input_arg = get_arg(sys.argv[1:])
print "process started at " +strftime("%Y-%m-%d %H:%M:%S")
print input_arg
i am running code like this
python main.py -i ok.txt -ds ds_val -dm dm_val -ts tas_val -tm tm_val
i want to parse all input arguments to a list. i imported all required modules to my script
now i am able to parse only -i input.How can i parse -tm,-ts ,-dm,-ds iputs?
In an interactive Python experiment with passing various argv arrays to getopt
>>> getopt.getopt(['-hd','1'],"hd:i:ds:dm:db:ts:tm:")
([('-h', ''), ('-d', '1')], [])
You did not tell it to look for an -hd option, but rather a -h and a -d that takes an argument.
Generally for multiletter options, argument parsers expect you to use --. Your getopt should be
>>> getopt.getopt(['--hd','1'],"i:",["hd=","ds=",...])
([('--hd', '1')], [])
But, do consider argparse.
You can manage using argparse, in just a few lines.
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input-file', '-i', type=str,
help='input file (eg: input_file.tar.gz)')
parser.add_argument('--staging-db', '-S', type=str,
help='staging database name')
parser.add_argument('--main-db', '-M', type=str,
help='main database name')
parser.add_argument('--staging-table', '-s', type=str,
help='staging table name')
parser.add_argument('--main-table', '-m', type=str,
help='main table name')
parser.add_argument('--debug', '-d', type=bool, default=False,
help="activate debug mode (defaults to False)")
args = parser.parse_args()
The parser help is generated by argparse. You can output it by typing
$ python YOURSCRIPT.py --help
need to replace filename in subprocess command i.e. filename
filename = '\'D\:/imagesequence/thumbnail.jpg\''
task = '\"movie= ' + filename + '[watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]\"'
c = subprocess.check_call(["ffmpeg", "-i", "D:/imagesequence/background222.jpg", "-vf", task, "D:/imagesequence/fwtm108.jpg"],shell=True)
this gives error
# Error: CalledProcessError: Command '['ffmpeg', '-i', 'D:/imagesequence/background222.jpg', '-vf', '"movie= \'D\\:/imagesequence/thumbnail.jpg\'[watermark];[in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]"', 'D:/imagesequence/fwtm107.jpg']' returned non-zero exit status 1 #
when put altogether it works well
c = subprocess.check_call(["ffmpeg", "-i", "D:/imagesequence/background222.jpg", "-vf", "movie= 'D\:/imagesequence/thumbnail.jpg'[watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]", "D:/imagesequence/fwtm101.jpg"],shell=True)
even this works
task = "movie= 'D\:/imagesequence/thumbnail.jpg'[watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]"
c = subprocess.check_call(["ffmpeg", "-i", "D:/imagesequence/background222.jpg", "-vf", task, "D:/imagesequence/fwtm102.jpg"],shell=True)
any different view. I am not able to see it.
q = "D\:/imagesequence/thumbnail.jpg"
task = "movie= '%s'[watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]" % q
c = subprocess.check_call(["ffmpeg", "-i", "D:/imagesequence/background222.jpg", "-vf", task, "D:/imagesequence/fwtm106.jpg"],shell=True)
this worked. thank you to
http://comments.gmane.org/gmane.comp.python.tutor/65858