The below code begins by open a new txt file for O/P and write a lines and close the file
Code:Write txt in file
>>>file=open(file.txt','w')
>>>file.write('hello python\n')
12
>>>file.write('python is programme best language')
33
>>>file.close()
Code :read txt in the file
>>>file=open('file.txt')
>>>file.readline()
'hello python\n'
>>file.readline()
'python is programme best language'
Read all at once string
>>>open('file.txt').read()
Output:
'hello python\npython is programme best language' //output is in friendly so we use
>>>print (open('file.txt').read())
hello python
python is programme best language //user-friendly display
If you want to scan a txt file line by line
>>>for line in open('file'):
... print(line, end= ' ')
hello python
python is programme best language
No comments:
Post a Comment