Play with Text Files : using Python by Koduri Bhargav

Play with Text Files : using Python by Koduri Bhargav

Author:Koduri Bhargav [Bhargav, Koduri]
Language: eng
Format: azw3, epub
Published: 2021-06-22T00:00:00+00:00


A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and the user has sufficient privilege permission to change the file.

2.7 Modify a Record

2.6.1 Copy the line that you want to modify

f = open("pd.txt", "r")

nf=open("newfile.txt","a")

s=input("Enter name to modify: ")

for line in f:

if s not in line :

nf.write(line)

else:

phNum= input("Enter the phone numer: ")

res=" "+s+" "+phNum+"
"

nf.write(res)

f.close()

nf.close()

2.6.2 Delete the original textfile

import os

os.remove("pd.txt")

2.6.3 Rename the new text file with the original text file



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.