Running an R script from command line (to execute from python) - python

I'm currently trying to run an R script from the command line (my end goal is to execute it as the last line of a python script). I'm not sure what a batch file is, or how to make my R script 'executable'. Currently it is saved as a .R file. It works when I run it from R.
How do I execute this from the windows command prompt line? Do i need to download something called Rscript.exe? Do I just save my R script as an .exe file? Please advise on the easiest way to achieve this.
R: version 3.3 python: version 3.x os: windows

As mentioned, Rscript.exe the automated executable to run R scripts ships with any R installation (usually located in bin folder) and as #Dirk Eddelbuettel mentions is the recommended automated version. And in Python you can run any external program as a subprocess with various types including a call, check_output, check_call, or Popen and the latter of which provides more facility such as capturing errors in the child process.
If R directory is in your PATH environmental variable, you do not need to include full path to RScript.exe but just name of program, Rscript. And do note this is fairly the same process for Linux or Mac operating systems.
command = 'C:/R-3.3/bin/Rscript.exe' # OR command = 'Rscript'
path2script = 'C:/Path/To/R/Script.R'
arg = '--vanilla'
# CHECK_CALL VERSION
retval = subprocess.check_call([command, arg, path2script], shell=True)
# CALL VERSION
retval = subprocess.call(["'Rscript' 'C:/Path/To/R/Script.R'"])
# POPEN VERSION (W/ CWD AND OUTPUT/ERROR CAPTURE)
curdir = 'C:/Path/To/R/Script'
p = subprocess.Popen(['Rscript', 'Script.R'], cwd=curdir,
stdin = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
output, error = p.communicate()
if p.returncode == 0:
print('R OUTPUT:\n {0}'.format(output.decode("utf-8")))
else:
print('R ERROR:\n {0}'.format(error.decode("utf-8")))

You already have Rscript, it came with your version of R. If R.exe, Rgui.exe, ... are in your path, then so is Rscript.exe.
Your call from Python could just be Rscript myFile.R. Rscript is much better than R BATCH CMD ... and other very old and outdated usage patterns.

You probably already have R, since you can already run your script.
All you have to do is find its binaries (the Rscript.exe file).
Then open windows command line ([cmd] + [R] > type in : "cmd" > [enter])
Enter the full path to R.exe, followed by the full path to your script.

Related

How to run perl script with multiple args from python script

I made a python code that must sequentially execute a series of perl commands on the PC shell, the problem is that I did not realize that to send these scripts I have to add parameters (i have n_params), advice?
example command to send
perl [file_name.pl] [params]
To run these commands on the windows CMD I am using os and subprocess
python code example
# command = perl [file_name.pl] [params]
# path = location/of/where/the/pl/file/is/saved
perl_script = subprocess.Popen(["C:\\Perl64\\bin\\perl.exe",path + command], stdout=sys.stdout)
perl_script.communicate()
But running the script like this, the code gets me wrong because it says it can't find the filename in the specific directory
This argument to Popen()
["C:\\Perl64\\bin\\perl.exe", path + command]
does not look correct since you wrote that command is perl [file_name.pl] [params]. Instead try:
p = subprocess.Popen(["C:\\Perl64\\bin\\perl.exe", path+"file_name.pl", "param1", "param2", ...])

Using python, how to find a different python script in a directory and then run it

Using python, I need to find another python script file in a directory and then run it
system = input("Enter system name: ")
for filename in listdir(directory):
if filename.find(system + "_startup") != -1 and filename.endswith(".py"):
# import and run specific startup script
I know how to find and open a file normally, and I know how to call one python script from another script, but I don't really know how to bridge the gap here.
Each system I'm working with will have a different "startup" script which runs. I don't want to have to import every single startup file I have into my main script (there's a lot of startup scripts) only the specific one for the system of interest. Is there a way for me to achieve this without importing all the startup files?
If all you need to do is execute the script, one option is to spawn a new process:
import subprocess
subprocess.run(["python3", filename])
Why not use os.system to execute the command in a subshell?
os.system('py -3 ' + filepath)
for catch output bash
import subprocess
from subprocess import Popen
system = input("Enter system name: ")
for filename in listdir(directory):
if filename.find(system + "_startup") != -1 and filename.endswith(".py"):
p = Popen(["python3",filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, errors = p.communicate()
# catch output bash
print(output)
# catch erro bash
print(errors)

How to run a powerShell script from a python file

I couldn't find much information on this unfortunately, but I wish to run a powerShell script from a python file I've written. I want the user to actually see the powerShell script being run and the user can enter inputs that the powerShell script requires from python. I am using pyCharm as an IDE.
When I run the script to call this powerShell script, it gives me this error:
File "C:\TestAutomation\eFuse\eFuse.ps1", line 19
SyntaxError: Non-ASCII character '\xc2' in file C:\Test\eK\eK.ps1 on line 19, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Here is the relevant part of the code:
elif switch_result == "eTool":
subprocess.call(['python', 'C:\\TestAutomation\\eFuse\\eFuse.ps1'], stdout=sys.stdout)
This elif statement is a part of other if/elif statements that run other python files using the subproccess module, but for some reason I can't get this powerShell file to be run. Any advice is appreciated. Thank you
First, you should already know .py file's interpreter is python.exe
So, it's easy to understand .ps1 file's interpreter is powershell.exe, not python.exe
I just copy & paste from yours, your code should look like following,
subprocess.call('powershell.exe -File "C:\\TestAutomation\\eFuse\\eFuse.ps1"', stdout=sys.stdout)
Details about powershell.exe -?
You can do following for provide input from user to PS (powershell) script from python :
1) Create parameterised powershell script
2) Take input in python and set it as PS script parameter.
3) run/execute the script with given params.
Here is the sample to run powershell script from python with param. Its a python code :
* Hope you have the PS script with parameters.
import subprocess
params = ['param1', 'param2'] # POWERSHELL SCRIPT PARAMETERS ( optional )
script_path = "C:\\PowershellScripts\\test.PS1" # POWERSHELL SCRIPT PATH
commandline_options = ["Powershell.exe", '-ExecutionPolicy', 'Unrestricted', script_path] # INITIALIZING COMMAND
for param in params: # FOREACH LOOP OF PARAMETERS
commandline_options.append(param) # ADDING SCRIPT PARAMETERS TO THE COMMAND
result = subprocess.run(commandline_options, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines = True) # RUN THE SCRIPT USING SUBPROCESS WITH PARAMS
print(result.returncode) # PRINT THE RETURN CODE FROM POWERSHELL SCRIPT
print(result.stdout) # PRINT THE STANDARD OUTPUT FROM POWERSHELL SCRIPT
print(result.stderr) # PRINT THE STANDARD ERROR FROM POWERSHELL SCRIPT
You can see the output of python if it exist ( if there is no error in powershell script. you will not get any output from last line )

Execute windows shell command and process output variables

In Python 3.7 running on Windows, what specific syntax is required to:
1. Navigate to a directory containing a terraform program
2. Execute "terraform apply -auto-approve" in that target directory
3. Extract the resulting output variables into a form usable in python
The output variables might take the form:
security_group_id_nodes = sg-xxxxxxxxxx
vpc_id_myvpc = vpc-xxxxxxxxxxxxx
Want to be using windows cmd style commands here, NOT powershell.
My first failed newbie attempt is:
import os
os.chdir('C:\\path\\to\\terraform\\code')
from subprocess import check_output
check_output("terraform apply -auto-approve", shell=True).decode()
Not sure about your output, but subprocess could definitely make the trick.
Try something like:
command = 'terraform apply -auto-approve'
TARGET_DIR = 'E:\Target\Directory'
subprocess_handle = subprocess.Popen(shlex.split(command), cwd=TARGET_DIR, shell=False, stdout=subprocess.PIPE)
subprocess_handle.wait()
result = subprocess_handle.communicate()[0]
print(result)
Worked for me once, just play around with params.
UPD: Here I assume that "terraform" is an executable.

Run a .bat file using python code

I try to run a .bat file in Windows using Python script.
ask.bat file:
Application.exe work.xml
I write Python code :
import os
os.system("D:\xxx1\xxx2XMLnew\otr.bat ")
Output: when try to run the file its just give a blink of the command prompt, and the work is not performing.
Note: I try with alternate slash also , but it is not working.
And I also want to save output of the file in another file.
Can anyone suggest how can I make the script runnable.
This has already been answered in detail on SO. Check out this thread, It should answer all your questions:
Executing a subprocess fails
I've tried it myself with this code:
batchtest.py
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
batch.bat
echo Hello World!
pause
I've got the batchtest.py example from the aforementioned thread.
import subprocess
filepath="D:/path/to/batch/myBatch.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
print p.returncode # is 0 if success
Replace \ with / in the path
import os
os.system("D:/xxx1/xxx2XMLnew/otr.bat ")
Probably the simplest way to do this is ->
import os
os.chdir("X:\Enter location of .bat file")
os.startfile("ask.bat")
It is better to write .bat file in such way that its running is not dependent on current working directory, i.e. I recommend to put this line at the beginning of .bat file:
cd "%~dp0"
Enclose filepath of .bat file in double quotes, i.e.:
os.system('"D:\\x\\so here can be spaces\\otr.bat" ["<arg0>" ["<arg1>" ...]]')
To save output of some batch command in another file you can use usual redirection syntax, for example:
os.system('"...bat" > outputfilename.txt')
Or directly in your .bat file:
Application.exe work.xml > outputfilename.txt
You are just missing to make it raw. The issue is with "\". Adding r before the path would do the work :)
import os
os.system(r"D:\xxx1\xxx2XMLnew\otr.bat")
So I do in Windows 10 and Python 3.7.1 (tested):
import subprocess
Quellpfad = r"C:\Users\MeMySelfAndI\Desktop"
Quelldatei = r"\a.bat"
Quelle = Quellpfad + Quelldatei
print(Quelle)
subprocess.call(Quelle)
python_test.py
import subprocess
a = subprocess.check_output("batch_1.bat")
print a
This gives output from batch file to be print on the python IDLE/running console. So in batch file you can echo the result in each step to debug the issue. This is also useful in automation when there is an error happening in the batch call, to understand and locate the error easily.(put "echo off" in batch file beginning to avoid printing everything)
batch_1.bat
echo off
echo "Hello World"
md newdir
echo "made new directory"
If you are trying to call another exe file inside the bat-file.
You must use SET Path inside the bat-file that you are calling.
set Path should point into the directory there the exe-file is located:
set PATH=C:\;C:\DOS {Sets C:\;C:\DOS as the current search path.}

Categories