Write a Python program to show the file writable or readable

python file handling

So, we can divide our post into four parts:

  • Check the file is Writable or Not
  • Python File Handling
  • Python Create New File
  • Python Read Files
  • Python Edit Files
  • Python Write Files And Delete the Existing Content  
  • Python Delete Files
  • Conclusion


To file Create, Reading, Update, Delete in python, We use the File handling function. And we make a program of all of this and we also understand how this work and in the end, we discuss, Where File Handling is used and how we can use it to overcome our work.

Check the File is Writeable or Not

To check the file is writeable or not, you can use the writable function.


file = open("name.txt", "a")

file handling

Python File Handling

File Handling is used to handling the file using python, You can create, read, update, and delete the file using python file handling.

First of all, we use the open() function. And this open() function takes two-parameter filename and mode, here mode means what to do with the file.

Four main types of modes:
For read, we user                     -- "r"
For creating the file we use    -- "x"
For write we use                     --"w"
For append the file we use      -- "a"

The syntax for file handling:
First, we will make a file in the same folder, for example, files.txt. Now we see how to implement this file in the python program.
file = open( "files", "mode" ) 

Python Create New File

We can create the file using the python program, And we use the "x" mode to create a file using python. And we can do other operations with these files in the next topic.

So our program uses the above syntax to create the file using "x" mode.

Our program:
file = open( "newfile.txt", "x" )
When you run this program, you will found the new txt file in the folder where this program exists. You found an empty newfile.txt file after running the program.

Python Read Files

So, First, we make a file in the same folder, for example, readfiles.txt. 
The readfiles.txt contain like this, you can write anything in the file, for example in below:

Welcome to do here blog.
Hello! I am Ankur.

Now we will make a program to read the above file And make sure you can save the file in the same folder. And if the file saves in a different folder you want to specify the folder in the program.
file = open( "readfiles.txt", "r" )
print ( file.read() )

If a file in a different folder:

file = open( "E:\\Desktop\dohere\readfiles.txt", "r" )
print ( file.read() )

And you see the output like this:

Welcome to do here blog.
Hello! I am Ankur.

Python Edit Files

In this case, we use "a" mode because "a" mode is used to overwrite the content without deleting the existing content.

So, our program first used "a" mode, and after we also use "r" to reading the update in the file. And we use our exiting file that's readfiles.txt.

file = open("readfiles.txt", "a")
file.write(" Now we will add more content in the file ")
file = open("readfiles.txt", "r")

Welcome to do here blog.
Hello! I am Ankur. Now we will add more content in the file.

Python Write Files And Delete the Existing Content 

Now we can use "w" mode to write the line of the file. But, In case the existing content or lines are deleted. And we will also use "r" mode to read the files and see the update of the files.

So our program is:

file = open("readfiles.txt", "w")
file.write("New content")
file = open("readfiles.txt", "r")


New content

Python Delete Files

To delete the file, first, we need to import the os. And we use the remove function to remove the file.


import os

This program can remove the file.txt file from your computer or laptop.


In these posts, we learn how to handle the file using python and how we do different work with python. And this file handling is used in the web application, And it makes your work fast. And in this post, we will also learn that python can create and delete the file and also update the file.

Next Post Previous Post
No Comment
Add Comment
comment url