Python Text File Questions: When you type a number, you add the numbers in the file to save them to the file

Asked 2 years ago, Updated 2 years ago, 80 views

Is there any code that can be modified by opening a text file (.txt)? If I type a number, I want to add the numbers in the file to make it possible to save it in the file, so I'd appreciate it if you could let me know!

txt python

2022-09-20 18:00

1 Answers

You can do it in the following:

# If there is only one number written in text.txt,
with open('text.txt', 'r') as txt:
    N1 = txt.read()   # N1 = '1'
N2 = int(N1) + 11    # N2 = 12
with open('text.txt', 'w') as txt:
    When you open txt.write(str(N2) #text.txt, the number 12 is entered


2022-09-20 18:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.