Write shell output into a text file [duplicate] - python

This question already has an answer here:
How to save the output of an IPython console to a file in Spyder?
(1 answer)
Closed 4 years ago.
How can I write the shell output in to a text file in python? I want to save the shell output as a log in a text file and I am currently using spyder.

Instead of just using print statement or print(statement), use:
a) some logging modules. Have a look at logging module for example.
b) an opened file for the output and write everything to that file. So in your script:
fout=open('log.txt','w')
# Here are your script things....
fout.write(statement+'\n')
# More script things ...
fout.close()
c) use an example from this post: How to save the output of an IPython console to a file in Spyder?

Related

Writing output of python print() to file appears after process is killed / finished [duplicate]

This question already has answers here:
How can I flush the output of the print function?
(13 answers)
Closed 2 years ago.
I had several print() statements in my code. I want run the code overnight and have them write to file.
I tried redirecting output to file as:
python mycode.py >> log.txt
While python process is running, I cannot see anything written to file. When I kill the process and open the log file, I find the output in it.
Similar behavior was seen when I explicitlys specified file handle in the every print statement:
output_file = open('log.txt','w')
print('blablabla', file=output_file)
How can I make print to write the file immediately? Am I missing anything?
You are not seeing your changes right away because in some cases, due to buffering, changes made to a file may not show until you close the file.
To save changes without closing, please, refer to this page Can I save a text file in python without closing it?
Can you try this and tell me if it updates, immediately or not. I'm always using this syntax in PyCharm. I hope it works with u, too.
If you want to insert bunch of data one at time.
with open("log.txt", 'w') as test:
print ("hello world!", file=test)
If you want to insert data into .txt, then do some process, then back again to add more data to the .txt
test = open("log.txt", 'w')
print ("hello world!", file=test)
#==== Do some processes ====
print("This's the end of the file")
test.close()

How to write "subprocess.run" results to a log file without using "shell=True"? [duplicate]

This question already has answers here:
How do I redirect stdout to a file when using subprocess.call in python?
(2 answers)
Closed 2 years ago.
Currently, I am using the following format to write the run results to a log file.
p = subprocess.run(["mpiexec -n 2 ./executor >log"],shell=True)
Could anyone tell me how to avoid using the "shell=True" while I can write a log file?
Thank you.
Just split the arguments yourself, open the file yourself, and pass the open file to run to make it send the output there:
with open('log', 'wb') as outfile:
p = subprocess.run(['mpiexec', '-n', '2', './executor'], stdout=outfile)

Suppressing output of a console application when invoked from Python script [duplicate]

This question already has answers here:
Suppress output in Python calls to executables
(9 answers)
Closed 2 years ago.
I'm working on a school project and I have to run John the Ripper through as a Python script.
Currently I'm running the command like this:
subprocess.run(['john --wordlist=/usr/share/john/password.lst --format=sha512crypt /root/Desktop/passwd '], shell=True)
My issue here is that it prints a lot of the process in the command terminal and I can't have anything show up in the terminal.
How can I make it so it doesn't print anything in the terminal?
You want to redirect the print so it dumps it in a file instead of printing on the terminal.
You might want to add something like after your code.
f = open("test.py", "w+")
print(output.stdout,f.write(output.stdout))
If you do cat test.py you should see the output.

os.system not writing in terminal [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 6 years ago.
i have a simple test file i created in order to use vmd (a program for my job)
This test file is as simple as :
import os
os.system("vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol")
Basically, im using os.system to launch a program name vmd with another script i wrote and im giving it one argument. What i found it is that when i run this test script, i get nothing done but if i just go in terminal and write :
vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol
everything works perfectly. Is there anything im doing wrong with os.system? I have been using this line for a while now but on linux and it was working perfectly, could it be a mac issue?
Thanks allot
import subprocess
ls_output = subprocess.check_output(['vmd', '-eofexit', '<', 'VMD_script.tcl', '-args', '3spi_cholesterol'])

using python subprocess's call function, but not getting output file [duplicate]

This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Python: How do I redirect this output?
(2 answers)
Closed 8 years ago.
I'm using python subprocess library to run command line in python file.
After importing the library, I used following code to store the output
call(["python", "make.py", ">", "data"])
But for some reason, I didn't get data file
You have to modify the stdout , check the official document subprocess
import subprocess
my_output_file = open("/home/user/output", "a")
subprocess.call(["python", "hello.py"],stdout=my_output_file)
my_output_file.close()

Categories