Skip to main content


Saving the last session in a text file and Continue (Reading and writing file)

See the video to understand:-


This is a small demo on how you can read old data from a file and write new data in the same file in for loop statement.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#Saving the last session in a text file
#And continue from where we left
import time
       
try:
       #Reading text file
       read_url = "text.txt"
       f = open(read_url, "r")
       lines = f.readlines()
       #print(lines[0])

       for i in range(int(lines[0]) , 60):
              print(str(i)+" -- "+" Seconds")
              f.close()
              g = open("text.txt","w")
              g.write(str(i))
              g.close()
              time.sleep(1)
       
except:
       #No text file? Then Create one
       f = open("text.txt","w")
       f.write(str('1')) #Giving it an input 1
       print("This file was not present so I have created one.")
       f.close()
       time.sleep(1)
       print("Re-Run the Script Now it will work the way you want.")


Hope you like the tutorial.

Python is simple and fun to play with and yet very powerful. 🔥


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Top 11 Essential Python Tips and Tricks

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram