shell command in python to run a specific windows executable - python

I have been searching but I am so confused. I so appologize if this has been answered before but I have looked and I am even more confused. All I want to do is run an executable file from a python script.
I know to open notepad.exe (for instance) you do the following.
#opens notepad in windows
import os
print os.system('notepad.exe')
but what if I want to run something specific in a directory
How can I effectively run something like this (this is obviously going to fail)
#opens program in windows
import os
print os.system('c:\files\program.exe')
the more I read about it, the more confused I get.
I have been reading about sys.executable as well as surprocess but its confusing me more than helping. Could someone clarify how this can be done? An example, perhaps to run a "program.exe" file?

You can use os.system like that. Note that strings require proper escaping though, so you might need to escape those backslash characters. Alternatively, you can also use a raw string to make it work:
os.system(r'c:\files\program.exe')

You can also use subprocess module https://docs.python.org/2/library/subprocess.html
import subprocess
subprocess.call('c:\files\program.exe')

Related

Problem running a R script from Python (Windows)

I've recently started programming in python for my job, so I'm quite new to it. My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3).
The problem comes when I have to call the R interpreter from python to run an R file that is generated (run.R file). The curious thing is that this only happens when I try to run my script in Windows, not in Linux. In both cases, I am trying to use the os module from python.
​
This is the code that is not working for Windows:
os.chdir(outRW) #first I change the working directory to the one where the run.R file is
os.system("C:\R-3.6.1\bin\Rscript run.R")
When I execute this, it changes the directory successfully, but when it comes to calling the R interpreter, it shows me this error:
The filename, directory name, or volume label syntax is incorrect.
However, I have tried running the "C:\R-3.6.1\bin\Rscript run.R" command in the Windows Command Prompt and it works perfectly.
I have also tried adding the path to R to the environmental variables, but again I could only make it work in the Command Prompt, not with python.
I guess there is something very obvious that I am missing here, but I cannot see it.
Any help or comments are very much appreciated!
Thank you!
Use double backslashes.
In R you need to use double backslashes \\, otherwise it'll try to interpret it as an Escape Character.
Use this and it will work:
os.system("C:\\R-3.6.1\\bin\\Rscript run.R")
Hope this helps.

How to save code at the python prompt in the terminal to a local file

I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.
I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.
I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.
So, how do save it?
Thanks!
See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.
>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
You can trying using another interpreter : bpython , I belive it has what you need,check it out.
Save the code you've entered to a file.
You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):
fire up with an initial workspace
modify by your liking
store the result
This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.
It really depends on your terminal for the exact commands.
The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.
But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...

How to run the Linux commands which is saved in a text file from python script?

Suppose I have a file 'commands.txt' which has list of linux commands line-by-line (for example: who, pwd, ls, ps, clear etc.,)
I need a python script wherein when run should execute all the linux commands one-by-one in the console/shell.
I am looking for different approaches to do it.
May be one approach I can think off is using os module in Python. (Please correct me if I missed out anything here)
import os
with open("commands.txt") as file:
for line in file:
os.system(line)
Kindly help me with different approaches to this problem.
-TIA
Like #papey said,
Create a bash script (.sh) file, with the correct shabang (#!/usr/bin/env bash).
You could write in your Python script the following then:
import os
os.system("chmod +x myscript.sh")
os.system("./myscript.sh")

Python: Is there any way I can take a script and import it's contents to idle in order to edit them there?

I have a little a script containing some "dictionaries".
"Is there any way I can take this script and import it's contents to idle?"* using a command like import.
What want to do then is to edit (or view) these dictionaries "live" in idle.....
Copy paste the script into the shell. Make sure there are no blank lines inside indented blocks or you'll get a SyntaxError.
Yes, import command should work: Just put "import myScript". You can add a path to your script with a line like sys.path.append(r"D:\myPath") before that statement
If you are on Windows right click on the script > Open with > Choose your Idle if it's there if not click on more(not sure what it's named it's the last option) then there should be some more options to open the file if your idle is still not there you can browse your files and go to the folder where your idle is saved and choose the exe to open the script in the Idle
Well I have to confess that I use linux mint, so there's no such thing as idle and that is why I use the terminal (where I type python3) to code instead! To make it clear, what I wanted to do is to take a script which contains a dictionary and some functions and then run it in terminal in order to use those functions there. The import command has nothing to do here!!!The answer is to use the exec commmand!!! Here is a picture:1. My question was not clear......
P.S Mushroommaula answered the question.

How do I execute this line of code in a python script as if it were command line?

I'm trying to write a script to duplicate running the line:
D:\...\bin>jython.bat D:\...\UploadTest.py
in a python script, like it were via cmd. I've tried a variety of subprocess.Popen commands and none of them work (all get the Errno2 file not found). I currently have
subprocess.Popen([r'D:\...\bin\jython.bat', r'D:\...\UploadTest.py'])
and I have tried several other variations. I'm mainly concerned with the space between the .bat and .py portions of the command. Should I just use r'' with a space between the two?
You probably need to use shell=True in the POpen call. But note the warnings about potential security issues found in the documentation.
I had tried something like this. Let me know if this suits your requirement. Here is the reference link
#!/usr/bin/env python
from subprocess import call
from textwrap import dedent
call(dedent("""\
#!/bin/bash
echo Hello world
"""), shell=True)

Categories

Resources