Passing arguments to a subprocess.run() in Python [duplicate] - python

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)

Related

In a windows command prompt, how do you make python translate a print command as an argument for a program? [duplicate]

This question already has answers here:
Batch equivalent of Bash backticks
(5 answers)
Hex input in Windows Command-line
(1 answer)
Closed 3 years ago.
I have built a very small C program that accepts arguments from the command line to experience with buffer overflows. I am trying to overwrite the stack's function return value with a specific hexadecimal value. Instead of looking through the ASCII chart, I know I can use python to translate hex values to ASCII for me.
I've only found documentation for Linux that havn't worked for me on Windows.
Run function from the command line
Input Hex into argv[1]
./myProgram $(python -c "print '\x48\x65\x6c\x6c\x6f'") (from above links)
What I have tried :
myprogram.exe ($python -c print('\xe9\x14\x40\x00'))
myprogram.exe $python -c print('\xe9\x14\x40\x00')
myprogram.exe python -c "print('\xe9\x14\x40\x00')"
Result should be:
myprogram.exe [argv1]
use python's print function to input value into argv1
myprogram.exe é[]#
[] is not a printable character. It's hex 14 in the ASCII table

Need to run shell command from Python script and store in a variable [duplicate]

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'])

"$#" in shell separates arguments with quotes even though it shouldn't [duplicate]

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?

Passing a variable to a subprocess call [duplicate]

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)

passing more than one variables to os.system in python [duplicate]

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'))

Categories