File Write in Python

Append (a ) : Add data at the end of the file. Existing data retained
Write ( w) : Add data at the beginning of the file. Existing data overwritten

Create a file and Write to it

If any existing file is there , it will be overwritten. A new file will be created.
fob=open('data1.txt','w')
fob.write("this is a new text just written")
fob.close()
If we don't want any existing file to be overwritten then we need to change the mode of opening of the file.
fob=open('data1.txt','x')
fob.write("this is a new text just written")
fob.close()
Above code will give error as data1.txt file is already there.

Let us write and then read the content.
fob=open('data1.txt','w')
fob.write("this is a new text just written")
## Writting over , now read
fob=open('data1.txt','r')
print(fob.read())
fob.close()
Output is here
this is a new text just written
File Read File Append

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer