I am not able to execute (or get the result) of commands executed in Windows by Python. I got a code sample I saw on various sites and answers in the OS, but none worked well for me.
import subprocess
# Exception: [WinError 2] The system cannot find the file specified
stdout = subprocess.check_call(['dir'])
proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell = True, bufsize = 1)
stdout, stderr = proc.communicate('dir c:\'.encode())
# Retorna sempre a mesma coisa
print(stdout)
Only return I have from the above print is always this:
b'Microsoft Windows [Version 10.0.10586] \ r \ n (c) 2015 Microsoft Corporation. All rights reserved. \ R \ n \ r \ nC: \ Users \ Daniel & More? '
I've tried to run as administrator. I've tried another machine. I've tried several different commands, dir
is just an example. I already tried changing the parameters of Popen
.
Note: The .encode()
I put in the communicate()
argument was on my own (I've already tried b''
). As I researched, Python recently changed the input pattern of several native string functions to bytes, although all examples I thought of this method were using string.