1

I try to execute command in cmd from Python and save the output. I have the following code:

import subprocess
import matplotlib.pyplot as pyplot
MyOut = subprocess.Popen("pytr portfolio",
            stdout=subprocess.PIPE, 
            stderr=subprocess.STDOUT)
stdout,stderr = MyOut.communicate()

chart_data = []
days = []
t = 0
new_char = stdout.split()
print(new_char)
netValue = float(new_char[new_char.index(b'netValue') + 2])

file = open('netValue.txt','a')
file.write(str(netValue)) 
file.write('\n')
file.close()

file = open('netValue.txt','r')
for line in file:
    t += 1
    print(line)
    chart_data.append(line)
    days.append(t)
file.close()
 
pyplot.plot(days,chart_data)
pyplot.title('depot_performance')
pyplot.show()

On my pc it works well but when I run this on my laptop i get the following error message:

Traceback (most recent call last):
  File "c:\Users\dinok\OneDrive\Desktop\Python_Scripts\depot_statistics.py", line 9, in <module>
    MyOut = subprocess.Popen("pytr portfolio",
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1438, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

I thought it is path related so that the module is not found but was not able to find a solution. What could cause this problem and how to fixt it?

Din
  • 33
  • 6
  • Does this answer your question? [open() gives FileNotFoundError/IOError: Errno 2 No such file or directory](https://stackoverflow.com/questions/12201928/open-gives-filenotfounderror-ioerror-errno-2-no-such-file-or-directory) – droebi Aug 28 '22 at 11:11
  • No, I have not really understand it yet. I think i need more time to go through it. – Din Aug 30 '22 at 16:15
  • I managed to fix it but i don't really know what was the problem. – Din Sep 20 '22 at 16:29

1 Answers1

0

Python's subprocess.Popen first parameter should be a list, the executable and the executable arguments. To fix it just do like the following snippet:

MyOut = subprocess.Popen(["pytr", "portfolio"],
                         stdout=subprocess.PIPE, 
                         stderr=subprocess.STDOUT)
lepsch
  • 8,927
  • 5
  • 24
  • 44
  • I still get the same error message. – Din Aug 29 '22 at 15:39
  • Most probably the `pytr` executable is not in the `PATH`. Did you activate the Python Virtual Environment? Did you install it with `pip`? – lepsch Aug 29 '22 at 16:28
  • yes i installed pytr with pip and I recieved a warning message that the directionary where pytr is installed was not in PATH. I coppied the directionary path and added it to PATH. But nothing changed. – Din Sep 01 '22 at 20:06
  • 1
    Try to restart the console/terminal window so the new environment variables take place. Also, make sure the path you've copied has `pytr` inside it. – lepsch Sep 01 '22 at 20:49
  • There are two dictionaries wich both contained pytr. I moved pytr from the one to the other folder and then it worked. – Din Sep 20 '22 at 16:28