it seems subprocess.call function just can be used for the files with '.exe' extension.
This is the code i tried for Firefox.lnk in which this is the same code i tried for a git program that has '.exe' extension and worked without error.
import subprocess
subprocess.call('C:/users/m.m/Desktop/Programs/Firefox')
This is the error I get with Firefox.lnk :
Traceback (most recent call last):
File "C:/Users/m.m/PycharmProjects/untitled5/pros.py", line 2, in <module>
subprocess.call('C:/users/m.m/Desktop/Programs/Firefox.lnk')
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 323, in call
with Popen(*popenargs, **kwargs) as p:
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 775, in __init__
restore_signals, start_new_session)
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 1178, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
Firefox (Without extension) gives me FileNotFoundError: [WinError 2] The system cannot find the file specified.
when i try the code without extension for those programs with '.exe' extension i have no problem but with any program without '.exe' extension i get error... just like firefox that has '.lnk' extension.
To process .lnk files on Windows there is os.startfile() (Windows only) in the standard library.
If you want to add parameters to the command, you can also use the start command. It is a builtin command (no start.exe) therefore a shell is needed to run it.
You can also use shell=True to make this work!
lnk_path = 'C:/users/m.m/Desktop/Programs/Firefox.lnk'
subprocess.call(lnk_path, shell=True)
Related
I try to run several simple command line commands on my system and although the code run i cannot see my window opening.
For example:
command = 'cmd'
os.system(command)
Why i cannot see my cmd window ? all i can see in the console window (i am using pycharm) is this:
Microsoft Windows [Version 10.0.18362.449]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\blabla\tmp>
I also try to open appium server using command line and i have .bat file that call appium (this is what i put into the .bat file and this work fine manually):
This is the path to my .bat file:
C:\tmp\scripts\start_appium.bat
Command
p = subprocess.Popen("start_appium.bat", cwd=r"C:\tmp\scripts")
stdout, stderr = p.communicate()
And i received this error:
Traceback (most recent call last): File
"C:\Python37\lib\subprocess.py", line 775, in init
restore_signals, start_new_session) File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo) File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.4\helpers\pydev_pydev_bundle\pydev_monkey.py", line
536, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args) FileNotFoundError: [WinError 2]
The system cannot find the file specified
Python documentation clearly mentions cwd param is not the directory to search the executives.
If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.
You can refer the documentation here.
https://docs.python.org/2/library/subprocess.html#popen-constructor
Also, try something like this
import subprocess
p = subprocess.Popen("C:\tmp\scripts\start_appium.bat")
I'm working on a Mac and I'm trying to open an external program (Visual Studio Code) using the subprocess module in Python. The code binary file is in usr/local/bin, which is in the PATH variable. My code works fine when I run it on a shell using python3.8 main.py. However, when I run the program using IDLE, Python keeps giving me
Traceback (most recent call last):
File "/Users/mike/Desktop/main.py", line 33, in <module>
subprocess.Popen(['code'])
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'code'
My code is as simple as it can get:
import subprocess
subprocess.Popen(['code'])
Does anyone know what's causing the problem and how I can fix it?
Thanks everyone.
If there are any extension, can you try with that while opening the file. Also we can try with providing the absolute path once.
try to use the full path of visual studio code, instead of just the word 'code'
subprocess.Popen(['usr/local/code'])
Big picture is I'm trying to automate my deployment process of building with maven and deploying to a web logic server. Little picture is I'm using subprocess to see if I can call maven from within python. When I attempt this subprocess mistakes mvn for a file.
Here is my code so far:
import subprocess
def main():
print(subprocess.check_output(["mvn", "-v"]))
if __name__ == '__main__':
main()
And here's my error:
C:\pythondev\python.exe "C:/pythondev/development/deployment scripts/redploy-to-localhost.py"
Traceback (most recent call last):
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 9, in <module>
main()
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 5, in main
subprocess.check_output(["a"])
File "C:\pythondev\lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "C:\pythondev\lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\pythondev\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\pythondev\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 1
Although my issue is with subprocess I'm open to answers that suggest a better alternative.
I ran into the same issue and was hesistant to use shell=True, because the internet tells me this is evil.
When I run where mvn in my cmd.exe, I can see that there are two matches:
mvn, which is a Unix shell-script (it starts with #!/bin/sh),
mvn.cmd, which is a Windows batch file.
I think what happens when you execute mvn something -something in cmd.exe is the following: Windows tries finding an executable called mvn. It finds the mvn file, but realizes that this file is not executable. It then tries finding files like mvn.com, mvn.exe, ... (see the %PATHEXT% system variable). When it finds mvn.cmd, it executes that and everyone is happy.
As far as I understand it, the problem with subprocess.check_output (and subprocess.run, and so on) is that the path-"expansion" via %PATHEXT% is not being performed. So the solution is that you have to give the extension manually and run your command as
print(subprocess.check_output(["mvn.cmd", "-v"]))
Try this it worked for me.
print(subprocess.check_output(["mvn", "-v"], shell=True))
I was wondering how I could use the script from this page in Python:
http://www.fmwconcepts.com/imagemagick/textcleaner/index.php
When downloading it, there is no extension. I tried renaming it to "textcleaner.sh" and run the following:
import subprocess
subprocess.check_call(['textcleaner.sh', '-g', '-e', 'normalize', 'input_image.jpg', 'output_image.jpg'])
But I get this error when doing so:
Traceback (most recent call last):
File "C:/Users/ikdem/PycharmProjects/McAffeeeeeee/bash test.py", line 11, in <module>
subprocess.check_call(['textcleaner.sh', '-g', '-e', 'normalize', 'opl-small-1.jpg', 'output.jpg'])
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 579, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 560, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\ikdem\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
On a windows system, the default shell isn't bash. Renaming to .sh won't help, and windows tries to execv your file, which isn't a Windows executable, which explains the (bad) error message from Windows.
The best thing you could do would be to:
download & install MSYS2 ex in C:\MSYS2
change your line as follows:
code:
subprocess.check_call([r'C:\MSYS2\bin\sh','-c','textcleaner.sh', '-g', '-e', 'normalize', 'input_image.jpg', 'output_image.jpg'])
Windows does not have support for parsing a Unix-style shebang within its binary loader, and will require you to invoke a copy of sh.exe or bash.exe instead, passing it the path to the script followed by arguments to the script. You can typically find these executables as part of a MinGW or MSYS installation.
You can run my script on Windows, only if you use a Unix terminal, such as installing Cygwin or using Windows 10 built in Unix and install ImageMagick.
If you run Python from a unix terminal on Windows, it can be run. See http://www.imagemagick.org/discourse-server/viewtopic.php?f=4&t=32920, where I run a simple Imagemagick command.
You can run it natively on Windows using a version of that script converted into Magick.Net. See https://github.com/dlemstra/FredsImageMagickScripts.NET
Usage of my script in either form for commercial purposes will require obtaining a license. Usage otherwise is free.
I am trying to learn how to run command line commands from Python. I am able to do this with DOS:
import subprocess
subprocess.call("dir",shell=True)
This is fine, but I need to be able to do this for linux commands because my company uses linux servers. I am using Mobaxterm to run a local linux session. When I try this:
import subprocess
subprocess.call("ls",shell=True)
I get this error from the terminal:
'ls' is not recognized as an internal or external command, operable program or batch file.
Which sounds ridiculous to me, because ls is clearly a linux command.
If I don't include shell=True I get this error:
Traceback (most recent call last):
File "ConsolePractice.py", line 4, in <module>
subprocess.call("ls")
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 560, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
So what am I doing wrong? How can I get this to work?
EDIT: Thanks for the comments. Python for Windows won't work for what I'm trying to do. I connected to a linux server and did:
cat > ConsoleCmd.py
import subprocess
subprocess.call("ls",shell=True)
^C
python ConsoleCmd.py
... and ls ran. Found the issue. I'll just have to get my scripts on to the server and run them from there. Thanks everyone
I was able to resolve this issue by opening an SFTP Session within Mobaxterm, transferring the python file to the server, then running the file directly on the server (python was already installed on the server).