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
Related
This question already has answers here:
Running Bash commands in Python
(11 answers)
Closed 8 months ago.
Basicaly, I would like to launch the perl script and use it as a python fonction in my programme.
My perl script contain an user input
use strict;
use warnings;
print "\nCan you hear me ? (1/0) ";
my $choice = <STDIN>;
if ($choice == 1){
print "\nYES";
}
I tried to us subprocess.run and subprocess.Popen with no success.
With subprocess.run, it print but doesn't wait the user's input.
PS: First post I something is missing just tell me ;)
import os
output = os.popen('perl filename.pl').read()
It assign everything to 'output' except the user input, but you can always print out that again inside the perl script and then it will work.
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:
Equivalent of Bash Backticks in Python [duplicate]
(11 answers)
Closed 8 years ago.
As far as you know, we can use OS console commands, (For example dir,time and format in Windows) in Python programing using os.system('TheCommand') module. But this function return the state of Operation (0 for successful and 1 for failed).
I want to know if is there any way to use the output of the commands in the next commands? I mean (For example) I run os.system('dir') and save the list of directories in a variable!
This is fairly easy to do. Here I define the working directory and the edit time of a file as variables which I used later in my script.
#!/usr/bin/env python
PWD = os.getcwd()
edit_time=os.path.getmtime(file.txt)
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)
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'))