python not displaying executable output properly
The code you are running would print a human-readable representation of a CompletedProcess
object which includes, but contains much more than, the actual output from your subprocess, on a single line.
Python 3.7.2 (default, Mar 25 2020, 10:15:53)
[Clang 11.0.3 (clang-1103.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> x = subprocess.run(['printf', '%s\n', 'foo', 'bar', 'baz'], capture_output=True)
>>> print(x)
CompletedProcess(args=['printf', '%s\n', 'foo', 'bar', 'baz'], returncode=0, stdout=b'foo\nbar\nbaz\n', stderr=b'')
To actually print only the output, try
>>> print(x.stdout.decode())
foo
bar
baz
Better yet, have Python decode it for you.
import subprocess
def executable_shell():
# to run cosmo file
x = subprocess.run(
# Just the command, as a list of arguments, so we can avoid shell=True
['./COSMO'],
# Specify which directory to run it in
cwd='../build',
# Throw an exception if the command fails (optional but recommended)
check=True,
# Have Python decode the output as text instead of raw bytes
text=True,
# Capture output, as before
capture_output=True)
return x.stdout
print(executable_shell())
Notice how I added text=True
(and also refactored to use check=True
and get rid of shell=True
) and how the function now returns the result, and the caller prints it (or does something else with it, as the case may be). Generally, you want functions to return results rather than print them; this makes it easier to reuse them for other tasks.
Check if a exe is run/openable or not and show output with python
The result from wait
is an error code; zero indicates success. You are basically inverting the logic, and calling the successes failures.
Furthermore, though, even if the result code indicates a failure, it means that the process actually ran. For example, on Unix-like systems, the false
command will report failure when you run it, by design; the absence of a result code is what indicates that running the command failed. In Python, this will always result in an exception.
As an aside, you should probably be avoiding Popen
; like the subprocess
documentation says, you should prefer run
or one of the other higher-level functions in every situation where you can avoid raw Popen
.
As a further aside, the print
is basically identical in all cases, and should simply be refactored to execute outside the condition. I also cleaned it up a bit. You never use notopened
so I took that out (it should be the difference between current
and opened
anyway).
files = glob.glob("*.exe")
total = len(files)
for current, fn in enumerate(files, 1):
try:
subprocess.run([fn])
opened += 1
whether = ""
except Exception:
whether = "not "
print("[%02i/%i] %s %sopened [%1.2f%%]" % (
current, total, fn, whether, 100*(total-opened)/total))
Anyway, randomly opening executables and waiting for them to finish is extremely risky. What if the executable formats the disk, shuts down the computer, or removes all files? You should probably be rethinking your approach.
Script doesn't produce any output after being converted to Win executable
It appears the pyinstaller has been launched from 3.6 location since I had it installed for 2.7 and 3.6. Once the correct location (C:\Python27\Scripts) has been pointed to, the script has been compiled successfully!
Why doesn't my code in test.py display anything when I import test?
>>> import test
>>> print(test)
<module 'test' from '/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test/__init__.py'>
There is a test
module in Python itself. Be sure you are running your module. You must be in the same directory as your file test.py
to import your file instead of the Python one.
Update: Python uses a search path list mechanism for finding modules when importing. The "current working directory" is usually at the first place in this list (the empty string). See PYTHONPATH
and sys.path
.
You can add your own path(s) into PYTHONPATH
(in shell/command line) or sys.path
(in Python).
pyinstaller output file running but not working correctly as expected
I just figured out that the problem occurred when I convert using -w
flag. Hence, to solve that I converted my .py
script to .exe
with the console (which means with out -w
flag)
so the command was : pyinstaller -F main.py
And in order to hide the console I added the following code to my main.py
:
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 0 )
python .exe not working properly
I had a very similar problem, the issue was a missing module(s). Your exe may use other modules that pyinstaller did not detect. For me, as soon as I imported Decimal module my exe worked like a charm! When your homepage.exe closes/crashes, it tells you which module is missing. You will only have a millisecond to see it, I recorded my screen and slowed down the footage to see the error message. Unconventional, perhaps, but worked for me!
Python server does not show output correctly
Now works. I added time.sleep(0.2)
after each sendall in rce.py
Python Script Output Does Not Appear in CMD
By the way, U said after you run "pip --version" nothing was displayed, I think this means you didn't install python correctly, pip should be installed automatically after you install Python. Just remind, make sure set the right Python path to your computer system path, set the "path" variable to the correct python istalled path.
In my opinion, you should uninstall python, and reinstall again.Any problem please let me know.
Related Topics
Command 'X86_64-Linux-Gnu-Gcc' Failed with Exit Status 1
Simulate Mouse Clicks on Python
Pip Is Not Working for Python 3.10 on Ubuntu
How to Create a User in Linux Using Python
Multiple Instances of Python Running Simultaneously Limited to 35
What Is the Easiest Way to Detect Key Presses in Python 3 on a Linux MAChine
Python Convert Microsoft Office Docs to Plain Text on Linux
Allowing Ctrl-C to Interrupt a Python C-Extension
Python: Interplay Between Lib/Site-Packages/Site.Py and Lib/Site.Py
Linux:Python:Clear Input Buffer Before Raw_Input()
Python's Equivalent for R's Dput() Function
Auto.Arima() Equivalent for Python
Converting Python Objects for Rpy2
Which Command to Use for Checking Whether Python Is 64Bit or 32Bit
Installing Python 2.7 Without Root
Error with Igraph Library - Deprecated Library