I want to open a file using the subprocess module as though the file was double-clicked in Explorer. How do I do that?
I tried the following line:
subprocess.call("C:/myfile.csv", shell=True)
which throws an error saying:
The syntax of the command is
incorrect.
'C:\' is not recognized as
an internal or external command,
operable program or batch file.
How do I emulate a double-click using subprocess? Basically I want to open a CSV file in Excel 2007.
os.startfile(r'C:\myfile.csv')
(Win32 only. For Mac, run a process with 'open filename'; on Linux/freedesktop-in-general, 'xdg-open filename'.)
I think part of your problem is you're using a unix style slash / as a path separator, instead of the windows backslash . It looks like windows is interpreting /myfile.csv as an argument for the program C:, which is why you're getting that message.
However if you corrected that, I think you'd just get it saying that C:\myfile.csv isn't a program.
I know that this is a bit late, but to do that in python 2.x (not sure about 3) you should use the subprocess module, referencing Popen. Here is the code:
import subprocess
subprocess.Popen(r'explorer /select, "C:\"')
It basically opens the file, and then opens it in the default program.
Related
I would like to run a .exe file on external terminal.
With this code I managed to run the .exe but it starts on the same terminal I call the script
import subprocess
subprocess.call(["C:/Users/Alessandro/Downloads/BOSCH-GLM/BOSCH-GLM/dist/glm50.exe"])
Is there a way to run it on external terminal?
Try using subprocess.Popen instead of subprocess.call.
Take a look at the discussion here. You can ignore the .communicate() call.
In a GUI app written in Python and Tkinter, I want to add a menu command which will open the source code of the *.py file in IDLE. I also want it to be platform independent.
I've tried using os.system to open IDLE from Scripts folder of Python, but that would be platform dependent. I couldn't find a way to get the Scripts folder of Python to make it independent. How can I achieve this?
To open a file in an IDLE editor, the command line is
<python> -m idlelib <path>
where <python> is the full path to a python executable or a name that resolves to such. If you want to open IDLE with the same python that is running you python app, which is likely what you want, use the platform-independent sys.executable. The path to the source code for the running app is __file__. This is one of the semi-hidden global variables when python runs a file. Or give the path to any other file.
Whether you use os.system or subprocess is a different issue. The latter is more flexible. subprocess.run replaces os.system. subprocess.Popen should not block. IDLE uses the latter to run user code.
I tried some special way:
import idlelib.pyshell
import sys
sys.argv.clear()
sys.argv.append("")
sys.argv.append(fName)
idlelib.pyshell.main()
It works! But I don't know whether this way will generate some problems.
A few days ago I had the line os.system(r"C:\Users\red\Desktop\Test UI") in my program. I tested it and it worked fine, it opened the application just like I wanted it to.
Now, I'm coming back to it about five days later and all of a sudden it's not working properly. I checked the processes and it says 'C:\Users\red\Desktop\Test' is not recognized as an internal or external command, operable program, or batch file.
I have already looked at the other questions about os.system like How do I execute a program from Python? os.system fails due to spaces in path, but I am already using a raw string like one of the answers suggested. I don't understand how it can work one day, and the next it fails to work with no change to it.
We've recently replaced os.system with subprocess.run after a number of problems with paths on Windows.
For this example, you could replace
os.system(r"C:\Users\red\Desktop\Test UI")
with
subprocess.run(r'"C:\Users\red\Desktop\Test UI"',shell=True)
For Windows shortcuts I had to add the .lnk extension in the call:
subprocess.run(r'"C:\Users\red\Desktop\Test UI.lnk"',shell=True)
I figured it out. For some reason, os.system stopped working consistently and subprocess.run or subprocess.call didn't work. I switched my command to use os.startfile instead and it started to work properly.
Here's the end result:
os.startfile(r"C:\Users\red\Desktop\Test UI")
Actually, you are using right command to execute a file, but the file you want to execute is not present in that directory. The error 'C:\users...' is not not recognized as an internal or external command, operable program, or batch file occurs when the file is not in the directory path.
There is no need to write .exe after file name, but you must write the correct file name which is present in that directory.
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.
I'm trying to run a Hello World program in Python I wrote in Notepad++ using the NppExec plugin, but instead of printing, I'm getting
python C:\Users\Sam\Desktop\Test.py
CreateProcess() failed with error code 2:
The system cannot find the file specified.
The argument I'm giving NppExec is
python C:\Users\Sam\Desktop\Test.py
which is the filepath that NP++ gives me when I copy the full filepath to the clipboard.
Is there some configuration of NP++ that I have to set to get this to work?
I tried what you are attempting to do, and this is how I solved it:
Instead of passing the argument you gave, I passed this one:
C:\Python32\python.exe C:\Users\Sam\Desktop\Test.py for python 3+
C:\Python27\python.exe C:\Users\Sam\Desktop\Test.py for python 2
Generally, in order for it to work, you have to define where you have installed the python executable.
In general, you can use the following as an argument to NppExec for any currently opened Python script in Notepad++:
[Your Python install folder here]\python.exe "$(FULL_CURRENT_PATH)"
Note that "FULL_CURRENT_PATH" is a Notepad++ internal variable, not a placeholder for your file's actual path and filename, so the above argument should work without edits regardless of your current script's filename.
Further references for using NppExec with other source code: http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Compiling_Source_Code
This is an alternative method to running python programs in notepad++, which I recommend after being unable to find a suitable plugin.
create a batch file called pythonXX.bat ( where XX is the current version of python you're using ) and save it along side your python.exe in C:\PythonXX\
and insert this text into that batch file:
#ECHO OFF
C:\PythonXX\python.exe "%1"
PAUSE
#ECHO ON
Then inside notepad++ create a run command:
C:\PythonXX\pythonXX.bat "$(FULL_CURRENT_PATH)"
Then click save run and assign it to a keyboard short-cut, good to go :)