简体   繁体   中英

print specific string result execution on python

ii only want to save specific print result of every execution of code on text file

i try with

if to,from_addr != '0x':
            print(To:,From:)
            os.system(f'echo {To:} {From:} >> output.txt')

but everytime fail, also I also want that every time a new result appears, a new line is added and not replace the later output

enter image description here

i try with

with open("Output.txt", "w") as text_file:
    print(f"To:" "From:" {to} {from_addr}", file=text_file)

Use append mode and fix f-strings:

to = 'destination'
from_addr = 'source'

# Use append mode
with open("Output.txt", "a") as text_file:
    if not (to.startswith('0x') or from_addr.startswith('0x')):
        print(f"To: {to}\nFrom: {from_addr}", file=text_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM