1

I would like to launch a process from a Python script on a raspberry pi 3 raspbian.

On Windows, the following code would work:

import os
def openFile():
    try:
        os.startfile("/home/pi/Desktop")
    except:
        print("invalid path")

But here I get back invalid path.

Can you tell me how to fix this and also how to open applications?

I remember that in windows: There's Notepad.exe in a path and I can open it but what should I say for Linux? I mean what is the .exe in Linux and can I open it?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
turd
  • 27
  • 4
  • 3
    Hi turd, welcome to StackOverflow! Could you please provide the exact error message you get? Also you should remove the try/except statement which hide the exact error message! – Adam Oudad Mar 21 '21 at 11:00
  • 1
    @turd Please - try to open the file (not the directory) (Desktop is a folder!) If your file is called `my_file.py` try `os.startfile("/home/pi/Desktop/my_file.py")` + **You are obviously NOT stupid. And - I am saying this because you are new: *Do NOT get discouraged by something that *might* sound offensive, in any stack site! It's just problem-pointing outs!* (As said, I just say this since you are new!) Good luck! – William Martens Mar 21 '21 at 11:15
  • 1
    So, I just tried `os.startfile` on my Windows machine and I was not familiar with it, but it seems like it launches the a process or window as if tha string was typied in the "Run" box. So I think the equivalent to that is one of many syntaxes for doing system calls, like `subprocess.run`, but I think you want one that doesn't wait on the process, am I right? – joanis Mar 21 '21 at 12:05
  • 1
    Also, the equivalent to `.exe` in Linux is making a file executable with `chmod`. Typing `ls` at the prompt finds `/usr/bin/ls` (usually) because `/usr/bin` is on your `$PATH` environment variable and `/usr/bin/ls` has its executable bit set. – joanis Mar 21 '21 at 12:07
  • 1
    Does this answer your question? [How to execute a program or call a system command from Python](https://stackoverflow.com/questions/89228/how-to-execute-a-program-or-call-a-system-command-from-python) – Tomerikoo Mar 21 '21 at 12:24
  • 1
    @WilliamMartens i got this (execpt:^ IndentationError: unexpected unindent) for this code import os def openfile(): try: os.startfile("/home/pi/python codes/draft.py") except Exception, e: print str(e) – turd Mar 27 '21 at 11:33
  • @AdamOudad actually nothings happens, i save the code i hit f5 the python shell opens and.... the code does nothing and is done. No error no nothing. all the answers in this page does this same thing. *maybe i'm cursed* – turd Jul 27 '21 at 13:13
  • 1
    @turd maybe this has to do with your code editor then. To check if your editor or environment is the culprit, I would recommend you to use an online python editor or an online jupyter notebook. Then you can try ask a question on a dedicated forum for the editor you are using (not in this thread I am afraid)... That's my way to repel curses at least ^^ – Adam Oudad Jul 29 '21 at 13:46

2 Answers2

2

os.startfile is only available for Windows. You should use instead the subprocess library. Try this platform-independant solution from @user4815162342 which I adapted

import os, sys, subprocess

def open_file(filename):
    if sys.platform == "win32":
        os.startfile(filename)
    else:
        opener = "open" if sys.platform == "darwin" else "xdg-open"
        subprocess.run([opener, filename])

If your file is just a bash script, you can replace the subprocess.run line by

subprocess.run(["bash", filename])
Adam Oudad
  • 337
  • 1
  • 9
  • This seems to work only since Python 3.5, right? Would it work with `subprocess.call` on older Pythons? – joanis Mar 21 '21 at 12:19
  • 1
    Yes, actually the answer I adapted was using `subprocess.call` instead, `subprocess.run` is recommended if you use a newer python version – Adam Oudad Mar 22 '21 at 06:17
2

As far as I understand, what you're trying to do is called not open but execute. So you could find more info about that by searching for "python execute file on Linux". Despite this, you are trying to execute a directory, not a file. So, here is an example of what I'd do:

import subprocess


subprocess.call(['/bin/ls', '-l'])

This will call the executable file ls located at the /bin folder and provide it with one argument: -l. It will list files in your current directory (however, remember that you shouldn't use ls for that purpose, this is just an example. If you want to list files in a directory, there are special functions for that in Python).

Speaking about file extensions, the executable file on Linux (similar to Windows' .exe file) is called an ELF file, which does not have a canonical extension. In fact, Linux in general cares about extensions much less, than Windows. If you want to know more about which else files can be executed on Linux, execute permissions etc, please, search for info on the internet and/or ask a question at https://superuser.com)

Kolay.Ne
  • 1,345
  • 1
  • 8
  • 23