May-29-2017, 11:58 AM
May-29-2017, 12:21 PM
In the console:
A single > writes to a file. If the file exists it will be overwritten. >> appends to a file. Both will create the file if it doesn't exist.
python3 my_script.py >> file.txtThis is called redirection. Instead of the standard output for any command ( stdout ) the output from the command is redirected to a file using >>
A single > writes to a file. If the file exists it will be overwritten. >> appends to a file. Both will create the file if it doesn't exist.
May-29-2017, 12:40 PM
Mike Ru Wrote:How can I write it into file?os.system is deprecated,use subprocess.
I have a script
For Python 2.7 or newer versions can use check_output(),to cacth the output.
Example:
from subprocess import check_output # Windows out = check_output(['ping', 'google.com']) # linux #out = check_output(['ping', '-c', '4', 'google.com']) print(out.decode('utf-8').strip())
Output:Pinging google.com [108.177.14.102] with 32 bytes of data:
Reply from 108.177.14.102: bytes=32 time=63ms TTL=48
Reply from 108.177.14.102: bytes=32 time=64ms TTL=48
Reply from 108.177.14.102: bytes=32 time=63ms TTL=48
Reply from 108.177.14.102: bytes=32 time=63ms TTL=48
Ping statistics for 108.177.14.102:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 63ms, Maximum = 64ms, Average = 63ms