You are on page 1of 3

new_age = '20'

with open('new_file.txt', 'w') as new_file:


with open('file.txt', 'r') as f:
lines = f.readlines()
for line in lines:
if 'Mo' in line:
new_file.write(line.replace(line.split(',')[2], ' ' + new_age))
else:
new_file.write(line)

newAge = "20"
result = ""
with open("file.txt") as f:
for line in f:
if line.lower().startswith( "mo," ):
list = line.split(', ')
list[2] = str( newAge )
line = ", ".join( list )
result += line + '\n'
f = open("file.txt", 'w') # should be in 'wt or 'w' mode
f.write(result)
f.close()

1. line_number: int # Number of the required line, 0-based


2. filename: str # The name of the file
3. line: str # Content of the required line
4.
5. with open(filename) as file:
6. for _ in range(line_number):
7. file.readline()
8. line = file.readline()
1. line_number = 5
2. line_i_am_interested_in = None
3.
4. with open(filename) as f:
5. line_i_am_interested_in = f.read().split(‘\n’)[line_number]
6.
7. if line_i_am_interested_in is None:
8. print('This file did not have a line {}'.format(line_number))

name = "Mahesha"

recordID =(int)(3)

after_update = ""

index = (int)(0)

with open("HardWareDetails.txt")as f:

for _ in range(recordID-1):

#index = index+1

print((str)(index))

if(index == (recordID-1)):

# print((str)(index)+"--------\n")

mylist =[]

mylist = line[index].split(',')

print(mylist)

mylist[3]= name

line = ','.join(mylist)

after_update=after_update+line+"\n"

else:

after_update=after_update+line+"\n"

print(after_update+"\n")

index = index+1
f2 = open("HardWareDetails.txt",'w')

f2.write(after_update)

f2.close()

You might also like