In this tutorial we are going to learn:

  • Writing Text Files
  • Creating a New File
  • Delete File

 

 

Writing Text Files

 

To write a file, we have to open the existing file with the open() method and we must add a parameter to the open () function :

  • ” a “   : Append  –  will append to the end of the file.
  • ” w “  : Write  –  will overwrite the content.

 

Example :

  • Writing to a file with ” a “

 

Output Of The Program : 

 

 

Example :

  • Writing to a file with ” w ” .


Output Of The Program : 

We overwrote all content.

 

 

 

 

Creating a New File

 

To create a new file we use the open() method and we must add a parameter ” x “ as a mode to our open() function.

Example : 

 

Output Of The Program : 

As you can see our new “newfile.txt” file created.

 

 

 

Delete File

 

To delete a file, we use os.remove() method.

 

Example :

 

Output Of The Program : 

As you can see our “newfile.txt” was deleted.

Leave a Reply

Your email address will not be published. Required fields are marked *