This question already has answers here:
How to use python variable in os.system? [duplicate]
(2 answers)
Closed 1 year ago.
I want to pass two variables to the os.system() for example listing files in different format in specific directory like (ls -l testdirectory) in which both a switch and test directory are variable.
I know for single variable this one works:
option=l
os.sytem('ls -%s' option)
but I dont know how to pass two variables?
you are asking about string formating (since os.system takes a string, not a list of arguments)
cmd = "ls -{0} -{1}".format(var1,var2)
#or cmd = "{0} -{1} -{2}".format("ls","l","a")
os.system(cmd)
or
cmd = "ls -%s -%s"%(var1,var2)
or
cmd = "ls -"+var1+" -"+var2
This, for example, works:
os.system('%s %s' % ('ls', '-l'))
Related
This question already has answers here:
How do I pass variable when using Python subprocess module [duplicate]
(2 answers)
Closed 1 year ago.
I am trying to pass an argument to a bash script I am running in Python using subprocess.run(). It is for a loop that runs the command passing in arguments from a list. I am using the Twint library but that part isn't important; I am pointing it out to make the question easier to answer. Currently, this is what I have
search_item = "bitcoin"
cmd = 'twint -s "%s" --limit 2000 --near "New York" -o file2.csv --csv' % (search_item)
p1 = subprocess.run( cmd , shell = True) % (search_item)
However on trying to run it, I get the error:
TypeError: unsupported operand type(s) for %: 'CompletedProcess' and 'str'
Any help on how I can pass the arguments? Thank you in advance
You don't need % (search_item) on the subprocess.run() line. You already substituted it when creating cmd on the previous line.
But there's nothing in your command that requires using shell=True, so you should put all the arguments in a list rather than a string. This is more efficient and avoids potential problems if strings contain special characters that require escaping.
search_item = 'bitcoin'
cmd = ['twint', '-s', search_item, '--limit', '2000', '--near', 'New York', '-o', 'file2.csv', '--csv']
subprocess.run(cmd)
This question already has an answer here:
How to pass multiple variable from php to python script
(1 answer)
Closed 2 years ago.
I found a way to get only one variable, And I need 18
I am currently using the code
echo shell_exec("C:/Python/python.exe C:/wamp64/www/site/test.py 2>&1");
The code Works great but how do I send variables to python?
You just pass them as command line arguments:
exec ( "C:/Python/python.exe C:/wamp64/www/site/test.py $var1 $var2 $var3" );
Then in your Python script you can read them like this:
import sys
print sys.argv[1] # first parameter
print sys.argv[2] # second parameter
print sys.argv[3] # third parameter
This question already has answers here:
Store output of subprocess.Popen call in a string [duplicate]
(15 answers)
Closed 3 years ago.
I have to run a shell command from python and get the output of that command into a python variable
python_var = subprocess.check_output('/opt/PPPP/QQQ/my_cmd -option1 -option2 /opt/XXXX/YYYY/ZZZZZ/my_file')
You need to hand-split the arguments into a sequence, not just pass a string (passing a whole string requires shell=True on Linux, but introduces all sorts of security/stability risks):
python_var = subprocess.check_output(['/opt/PPPP/QQQ/my_cmd', '-option1', '-option2', '/opt/XXXX/YYYY/ZZZZZ/my_file'])
This question already has answers here:
Bash: space in variable value later used as parameter
(4 answers)
How can the arguments to bash script be copied for separate processing?
(2 answers)
Closed 5 years ago.
I'm trying to parse arguments in a shell script for a python program. We're using a wrapper who takes the command line arguments and calls the correct python module.
For some reason, it keeps splitting up args I'm passing with double quotes. This is how I get the args:
args="$#"
Then I run:
runner.sh --host="XHOST 1"
or
runner.sh --host "XHOST 1"
the "$#" breaks the "XHOST 1" into 2 separate tokens of XHOST and 1.
First I've tried using the "$*" as always, but it didn't work. Now I'm using $# and it keeps splitting the args. This was tested on a rhel-7.2 machine.
Is there another way to parse shell args to try and keep them as one token when they are wrapped in quotes? Am I missing something here?
This question already has answers here:
Why does passing variables to subprocess.Popen not work despite passing a list of arguments?
(5 answers)
Closed 1 year ago.
I'm trying to cut the first n lines of a file off a file, I calculate n and assign that value to myvariable
command = "tail -n +"myvariable" test.txt >> testmod.txt"
call(command)
I've imported call from subprocess. But I get a syntax error at myvariable.
There are two things wrong here:
Your string concatenation syntax is all wrong; that's not valid Python. You probably wanted to use something like:
command = "tail -n " + str(myvariable) + " test.txt >> testmod.txt"
where I assume that myvariable is an integer, not a string already.
Using string formatting would be more readable here:
command = "tail -n {} test.txt >> testmod.txt".format(myvariable)
where the str.format() method will replace {} with the string version of myvariable for you.
You need to tell subprocess.call() to run that command through the shell, because you are using >>, a shell-only feature:
call(command, shell=True)