How do I embed Perl code in Python? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a Perl script I want to embed into Python. Whenever I run Python the Perl code also should execute. Both codes are in same file. I am running from HPSA so there is no filename associated with code run.

Something like this?
from subprocess import call as sp_call
perlscript = """
use strict;
use warnings;
print "EHLO world\n";
"""
exitcode = sp_call(['perl', '-e', perlscript], shell=False)
If you need to capture the output from the Perl script etc, look at subprocess.Popen and friends.

Related

How to restart a .py file when it finishes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am creating a simple game that will have the character die a lot, and as a consequence will end the game. how can i make it to have something like pressing the space bar restarts the file or something along those lines? It's a very simple file, so i am just looking for a simple solution.
You could wrap your script in a
while True:
...
block, or with a bash script:
while true ; do
yourpythonscript.py

R source function convert to Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
in my understanding, source in R is import and execute the code from source code. does Python has the equivalent function to import and execute.
Yes, you can import code/modules/etc using import in python. Here are some resources to get you started:
https://medium.com/code-85/a-beginners-guide-to-importing-in-python-bb3adbbacc2b
https://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial/Importing_Moduleshttps://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial/Importing_Modules
https://developers.google.com/edu/python/introduction
(If you know what you're doing, you may actually be looking for exec(open('filename').read()))

How i can call some functions when user in raw_input in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How i can run codes ( for e.g print('hello, world') ) when user in raw_input("Enter your value")?
I just want run some codes when user writing input from raw_input function.
For e.g when you are typing your message in chat room, you can see messages of other users also.
raw_input is blocking. You would need to use multiple threads or processes to make that work, and any print-like output would clobber what you were writing on the same line. If you wanted to make a chatroom-like experience, I wouldn't recommend it. You could try curses if you wanted to stay in terminal-land.

os module of python regarding os.linesep [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Why has os.linesep() been used here in my code?
def GetInstruction():
print(os.linesep)#<--------???
Instruction = input("> ").lower()
return Instruction
I don't know why you're using it, but from the docs:
The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for Windows.
Basically represents the current line separator default for your current environment or platform.

Translation of Shell Script in to Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to write a python code to run the job of the following Shell script for me:
for run in {1..100}
do
/home/Example.R
done
The shell script is basically running an R script for 100 times. Since I am new to python can somebody help me to write this code in python?
You could use subprocess.call to make an external command:
from subprocess import call
for i in xrange(100):
call(["/home/Example.R"])
You can use python's commands module to execute external commands and capture their output.
The module has a function commands.getstatusoutput(cmd), where cmd is the command you are looking to run, as a string.
Something like this could do the trick:
import commands
for x in xrange(100):
commands.getstatusoutput("/home/Example.R")
Each iteration of the for loop, the commands.getstatusoutput() function will even return a tuple (status, output), where status is the status after executing the program, and output being anything that the command would have written to stdout.
Hope that helps.

Categories

Resources