how launch a Perl script from python [duplicate] - python

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.

Related

Is it possible run a string in Python [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 1 year ago.
Is it possible to run a String text?
Example:
str = "print(2+4)"
Something(str)
Output:
6
Basically turning a string into code, and then running it.
Use exec as it can dynamically execute code of python programs.
strr = "print(2+4)"
exec(strr)
>> 6
I will not recommend you to use exec because:
When you give your users the liberty to execute any piece of code with the Python exec() function, you give them a way to bend the rules.
What if you have access to the os module in your session and they borrow a command from that to run? Say you have imported os in your code.
Sure is, looks like your "Something" should be exec.
str = "print(2+4)"
exec(str)
Check out this previous question for more info:
How do I execute a string containing Python code in Python?

Send variable's from PHP to Python? [duplicate]

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

How to get exact match while writing dataframe in file in pyspark using escape or quote? [duplicate]

This question already has answers here:
Propagate all arguments in a Bash shell script
(12 answers)
Closed 2 years ago.
I am trying to load Data frame into file but not able to get exact match. Can you please help me on this?
example:
"From...............\"dawood\"...........\"oral use\"........"
but i am getting:
"From................\"dawood\"...........\\"oral use\\\"......"
i am using below code to write the dataframe:
df.repartition(1).write.format('com.databricks.spark.csv').mode('overwrite').save(output_path,quote='"', sep='|',header='True',nullValue=None)
Can you please help me how to get exact match for all the reords.
either just copy this inside your shell script:
python imed_consump.py 'Smart Source'
but then your parameter is always fixed. should this not be desired, then do following inside shell
python imed_consump.py "$1"
and execute your shell like:
bash imed_consump.sh 'Smart Source'

How to read a line in the terminal with python [duplicate]

This question already has answers here:
Capture stdout from a script?
(11 answers)
Closed 3 years ago.
I'm trying to get a already written line in python 3, but I haven't found any function that can read a line from the terminal. It should work something like sys.stdout.read(), or sys.stdout.readline() but this function just throws an error.
If you mean to read from the user/a pipe, then simply use input.
However, from your comments it seems like you want to be able to read from what has already been printed.
To do this, you have a few options. If you don't actually want it to display on the terminal, and you only care about certain part of the output, then you can use contextlib.redirect_stdout and contextlib.redirect_stderr. You can combine this with io.StringIO to capture the output of your application to a string. This has been discussed in the question Capture stdout from a script in Python
However, if you want to have something which provides you both a means of printing to the terminal and giving you the lines, then you will need to implement your own type which inherits from io.TextIOBase or uses io.TextIOWrapper.
Do you mean something like this?
name = input("Enter a name: ")
print(name)

use os.system('MyCommand') in the next command [duplicate]

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)

Categories