-1

I could call the exe file on the windows system but not on ubuntu. I do not know what the error. Windows is working fine.

Python version:

dashzeveg@ubuntu:~/folder1$ python3 -V Python 3.5.2

My code:

import subprocess, sys
from subprocess import Popen, PIPE
exe_str = r"\home\dashzeveg\folder\HLR.exe"
parent = subprocess.Popen(exe_str,  stderr=subprocess.PIPE)

The error is:

dashzeveg@ubuntu:~/folder1$ python3 call
Traceback (most recent call last):
  File "call", line 4, in <module>
    parent = subprocess.Popen(exe_str,  stderr=subprocess.PIPE)
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '\\home\\dashzeveg\\folder\\HLR.exe'
tripleee
  • 175,061
  • 34
  • 275
  • 318
dashka
  • 29
  • 6
  • 2
    Expecting a Windows program to work natively on LInux? – ernest_k Apr 14 '19 at 06:14
  • I do not think the operating system is relevant. – dashka Apr 14 '19 at 06:17
  • import subprocess, sys from subprocess import Popen, PIPE exe_str = r"\home\dashzeveg\folder\HLR.exe" parent = subprocess.Popen(exe_str, stderr=subprocess.PIPE) This code – dashka Apr 14 '19 at 06:19
  • 1
    The backslashes in the path are also not going to work at all, you are looking for a file named `\home\dashzeveg\ ` etc in the current directory. Forward slashes are portable from Unix to Windows but backslashes the other way around very much not. But if the first comment is correct you also have a more fundamental misconception. – tripleee Apr 14 '19 at 06:19
  • 1
    The path separator in Linux is a forward slash, not a backslash. Try `r"/home/dashzeveg/folder/HLR.exe"` instead. – eumiro Apr 14 '19 at 06:19
  • Calling a Linux program `HLR.exe` is unusual, where did you get this from and does it run on your Linux system outside of Python? – tripleee Apr 14 '19 at 06:21
  • Dear tripleee. It's written in C #. However, using FTP is included in linux. – dashka Apr 14 '19 at 06:26
  • I changed the file as \ / but it does not work – dashka Apr 14 '19 at 06:27
  • *How* does it not work? Same error message? Or a different one? – Ignatius Apr 14 '19 at 06:29
  • I think the same error dashzeveg@ubuntu:~/folder1$ python3 call Traceback (most recent call last): File "call", line 4, in parent = subprocess.Popen(exe_str, stderr=subprocess.PIPE) File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'r/home/dashzeveg/folder/HLR.exe' – dashka Apr 14 '19 at 06:33
  • 1
    Did you put the 'r' inside the quotes? It should come before the quote. And also, can you confirm if you can open that file from terminal? Try running simply `/home/dashzeveg/folder/HLR.exe` on terminal. – Ignatius Apr 14 '19 at 06:36

1 Answers1

0

First check your slashes... they should be forward slashes for Linux (Windows will work with either btw).

Second... if you do not pass the command with "shell=True" when using subprocess module, then the command must be passed in as a list instead of a string (if the command has multiple options etc. they must be broken into individual elements). In your case it's easy... there's no spaces in your command so it will be a 1 element list.

Also since you're using Popen, I'm assuming you want the output of the command? Don't forget to decode() the output to a string from a binary string.

Here is an example:

james@VIII:~/NetBeansProjects/Factorial$ ls
factorial.c  factorial.exe
james@VIII:~/NetBeansProjects/Factorial$ ./factorial.exe 
5
120
james@VIII:~/NetBeansProjects/Factorial$ /home/james/NetBeansProjects/Factorial/factorial.exe 
5
120
james@VIII:~/NetBeansProjects/Factorial$ 
james@VIII:~/NetBeansProjects/Factorial$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen, PIPE
>>> cmd = ["/home/james/NetBeansProjects/Factorial/factorial.exe"]
>>> output = Popen(cmd, stdout=PIPE).communicate()[0].decode()
>>> output
'5\n120\n'
>>> output.strip().split()
['5', '120']

This is assuming your exe will really run on Linux. I just named that factorial.exe for the sake of clarity and for answering your question. Typically on Linux you won't see a file named factorial.exe... it would just be factorial.

If you really want to pass the command as a string for some reason. You need the shell=True parameter:

>>> from subprocess import Popen, PIPE
>>> cmd = "/home/james/NetBeansProjects/Factorial/factorial.exe"
>>> output = Popen(cmd, stdout=PIPE, shell=True).communicate()[0].decode().strip().split()
>>> output
['5', '120']

You could also do this with the os module too... frowned upon by some in the community, but much cleaner in my opinion (and less code):

>>> from os import popen
>>> cmd = "/home/james/NetBeansProjects/Factorial/factorial.exe"
>>> out = [i.strip() for i in popen(cmd)]
>>> out
['5', '120']
  • Notice though that `Popen` merely *starts* the process. As suggested in the `subprocess` documentation, you should always prefer a higher-level wrapper like `subprocess.run()` unless you really have to understand and manage a bare `Popen` object yourself. See also https://stackoverflow.com/a/51950538/874188 – tripleee Apr 14 '19 at 08:58
  • Meh I suppose... but many posts suggest otherwise: https://stackoverflow.com/questions/6657690/python-getoutput-equivalent-in-subprocess .... I prefer Popen because it works the same in 2 & 3 while run() does not. I would use Popen to save stdout over run(). run() is good if you are on Python 3.5 or higher and don't want to do anything else while the process is running. run() is actually doing Popen behind the scenes I believe. –  Apr 14 '19 at 18:54
  • "Higher-level wrapper like `run()`" also includes other higher-level wrappers like `check_output()`. The original higher-level wrapper `call()` has existed ever since `subprocess` was introduced in Python 2.4, though it does not support checking the exit status or capturing the output. – tripleee Apr 14 '19 at 19:48
  • Application tried to create a window, but no driver could be loaded. Make sure that your X server is running and that $DISPLAY is set correctly. err:systray:initialize_systray Could not create tray window Unhandled Exception: System.TypeLoadException: Could not load type 'HLR.Form1' from assembly 'HLR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type 'HLR.Form1' from assembly 'HLR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The following error has occurred – dashka Apr 15 '19 at 01:49
  • 1
    I could be wrong... but I think you need to recompile your code on Ubuntu, and then try to run it. In other words... try to run it without Python first.. just as an executable. If that doesn't work it definitely needs to be recompiled for Linux –  Apr 15 '19 at 02:57