Mac subprocess run with full path doesn't work - python

when I run the followin command in terminal it works, and starts application.
/Users/Someone/Documents/SomeApp\ v2.app/Contents/MacOS/SomeApp
But when I do the following in python, then run it:
import subprocess
subprocess.run(['/Users/Someone/Documents/SomeApp\ v2.app/Contents/MacOS/SomeApp'])
I get the following error msg.:
File not found error: [Errno 2] No such file or directory:
'/Users/Someone/Documents/SomeApp\\ v2.app/Contents/MacOS/SomeApp'

It didn't need the '\' in the string.

Related

No such file or directory when running a python script in WSL

I'm trying to run a python script in WSL that runs Ubuntu.
In WSL, the following command does not work:
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe /mnt/c/Users/admin/repo/script.py
C:\Users\admin\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file '/mnt/c/Users/admin/repo/script.py': [Errno 2] No such file or directory
However, the following individual commands work:
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe repo/script.py
admin:/mnt/c/Users/admin$ /mnt/c/Users/admin/repo/env/Scripts/python.exe
admin:/mnt/c/Users/admin$ cat /mnt/c/Users/admin/repo/script.py
Why am I seeing this strange behavior? How do I run a python script using absolute path?

Subprocess: Permission and No such file or directory error messages

I'm using Python 3.7 with Idle on a Mac running OS 10.15.4 and am trying to learn the basics of subprocess, with little success despite much effort.
import subprocess
subprocess.Popen('/Applications/Safari.app')
This results in a lengthy error message ending with
PermissionError: [Errno 13] Permission denied: '/Applications/Safari.app'
Removing the first backslash and instead using
subprocess.Popen('Applications/Safari.app')
results in
FileNotFoundError: [Errno 2] No such file or directory: 'Applications/Safari.app': 'Applications/Safari.app'
When I replace Safari with TextEdit, which I'm more interested in using, I receive the second of these error messages regardless of whether I include a backslash.
Use the full path to the executable:
subprocess.Popen('/Applications/Safari.app/Contents/MacOS/Safari')
<subprocess.Popen object at 0x1023414f0>
Alternatively there is the webbrowser module:
>>> import webbrowser
>>> s = webbrowser.get('safari')
>>> s.open('https://google.com')

python subprocess to open excel file

The goal is to open a particular excel file using a python shell subprocess. The code cannot be more simple yet I cannot figure out what is wrong:
import subprocess
arg1 = "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE"
arg2 = "C:\\Users\\user\\Documents\\test.xlsm"
p = subprocess.Popen(["start", arg1, arg2], shell=False)
The command works perfectly directly on the shell but when done through subprocess throws the following error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have also tried the below which works equally fine directly on the shell but the behavior is different:
p = subprocess.Popen([arg1, arg2], shell=False)
The following failed assertion pops up from Excel:
snapshopt error
My next try was:
import os
os.system("C:\\Users\\user\\Documents\\test.xlsm")
Which replicates the same assertion error as above plus returns a code 3 which based on
System Error Codes (0-499)
is a path not found.
Again same path works on shell, at this stage I run out of ideas, any help?
os.startfile is actually a better way to do it. As specified in the documentation -
os.startfile(path[, operation])
Start a file with its associated application.
When operation is not specified or 'open', this acts like
double-clicking the file in Windows Explorer, or giving the file name
as an argument to the start command from the interactive command
shell: the file is opened with whatever application (if any) its
extension is associated.
When another operation is given, it must be a “command verb” that
specifies what should be done with the file. Common verbs documented
by Microsoft are 'print' and 'edit' (to be used on files) as well as
'explore' and 'find' (to be used on directories)
You can just do the following -
import os
os.startfile("C:\\Users\\user\\Documents\\test.xlsm")

Python RPi - File not found when running script from another script

I'm trying to run a python script from another python script on a Raspberry Pi 3 with Raspbian. I've been trying to find ways to do this for some hours and didn't find anything that worked. I've tried some ways but it either says that has not permission to execute the file or it can't find it. I don't know what I'm doing wrong. I need to run multiple instances of the other script through the main script in a new console (new processes) and keep them running (I don't expect them to return anything to the main script). Can anyone help me? Because with Windows it was really easy as the program was working fine until I tried to run it on Linux (with Windows, I used os.startfile).
In test.py:
print("test1")
input()
In main.py:
import os
import subprocess
print("main")
os.system("python test.py")
input()
In the console:
main
python: can't open file 'test.py': [Errno 2] No such file or directory
In main.py:
import os
import subprocess
print("main")
subprocess.Popen("python test.py",shell=True)
input()
In the console:
main
python: can't open file 'test.py': [Errno 2] No such file or directory
In main.py:
import os
import subprocess
print("main")
subprocess.call("python test.py",shell=True)
input()
In the console:
main
python: can't open file 'test.py': [Errno 2] No such file or directory
I tried more ways but I don't remember them. Maybe I'm doing something wrong?
EDIT: I can now run the scripts without any problems with os.chdir (thanks to J H). My problem now is that it prints test in the same console window as the main.py and I needed it to create another process for the test.py. Any solutions?
EDIT 2: Finally I could start a new processes of the test.py from the main.py! I used os.system('xdg-open "test.py"') to open test.py with the default application. Anyway thanks to J H, otherwise it would continue to say file not found.
Final main.py:
import os
print("main")
os.chdir('/home/pi/Desktop/')
os.system('xdg-open test.py')
input()
Thanks in advance!
Printing out os.getcwd() will help you to debug this.
Either supply a fully qualified pathname, /some/where/test.py, or use os.chdir('/some/where') before executing test.py.

"No such file or directory" error when calling fc-list in python

I am attempting to scrape a terminal window of the list of fonts installed on the curent hosting server. I have written the following code:
import subprocess
cmd = 'fc-list'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]
but when i call this code, an exception is raised:
[Errno 2] No such file or directory
I can open a terminal window, and this works fine. What am i doing wrong?
You need to provide the absolute path to the executable. When you open a terminal window you then have a shell running which will search in $PATH to find the program. When you run the program directly, via subprocess, you do not have a shell to search $PATH. (note: it is possible to tell subprocess that you do want a shell, but usually this leads to security vulnerabilities)
Here is what you would want to use:
import subprocess
cmd = '/usr/local/bin/fc-list'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]

Categories