Friday, 13 September 2013

How to use File Action in Python

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.write('hello python\n')
>>>file.write('python is programme best language')

Code :read txt in the file


'hello python\n'


'python is programme best language'

Read all at once string



'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