In this article, I will share all kinds of Python file handling systems like reading files, writing, creating files, deleting processes and more. Stay here and read attentively to learn more and acquire knowledge in the Python file handling system. So let’s get started –
KeyPoint of the Content
- File Handling of Python
- File type in python
- Opening and Closing process of file
- The writing system of the python file
- Creating new file
- Deleting process File and Folder
- File methods of Python
File handling of Python operations
Python supports any kind of file handling and maintains its users. It is most important to use when you need to keep a file permanently in the storage.
In python, we used 6 types of operations that can be handled by a python file.
Now I will describe all of the above operations with an example below. Just follow these to know more about and practice this in your real life.
Create and Open files in Python
Python helps to open a file because this is a built-in method. Now I share how to create a file in python. It is enough to separate the name of the python file.
Example of python file –
f = open (“newfile1.txt”)
Complete open a file then you need to save this under your .py.
Saving system of python opening file –
f = open (“ newfile.txt”, “rt”)
Here ‘r’ for Reading and ‘t’ for text is the default values.
Closing file in python
Completed the performing operation of the file then you need to close it. For closing any file you need to use the close ( ) mode that is available in python.
Example of close methods –
f = open (“newfile.txt”, encoding = ‘utf-8’)
# provide services file operations
Read ( r ) file in Python
When you need to read any file under python it returns the whole text. If you want you can separate your needed characters here. By default, python used read () methods ( r )
Example of reading () file –
f = open ( “newfile.txt”, “r”)
Here you also read one line of the text file. Just include these codes under the .py file.
f = open(“newfile.txt”, “r”)
Creating a file using write () methods
Create a file, here you need to write some essential elements that explain why you used to write the ( w ) mode of python. Also, declare the working process of append (a) mode in python.
Example of write () mode in python –
f = open (“newfile1.txt”, “a”)
f.write( “This file has more contents!” )
# open file after appending :
f = open (“newfile1.txt”, “r”)
Deleting a file in python
If you want to remove a file under your .py folder. Here you must import OS modules. It can be used os.remove()
Example of deleting a file –
There are another two types of files in the python programming language. That is –
- Text file – This is the normal text that can open any kind of editor. Text files don’t have any encoding system.
Example of text file of python –
- Web standard – HTML< CSS< XML< JSON and more.
- Documents – csv, tsv and more.
- Configuration – int, reg, cfg and more.
- Binary file – When the file contains binary data that is called a binary file. It is not readable and uses basically a computer directory.
Example of a binary file in python –
- Documents – .doc, .pdf, .xls and more.
- Database – .frm, .mdb, .accde and more.
- Audio – .mp3, .mka, .aac, .wav and more.
- Video – .mp4, 3gp, .avi and more.
- Image – .png, .jpg, .gif, .bmp and more.
- Archive file – .zip, .iso, .7z and more
File Methods in the Python
|File Name||Explanation of methods|
|Open()||Open a new file|
|Write()||Write a string data in the new file|
|Writelines ()||Write a listed string data in this file|
|Fileno()||Provides an integer number of this file|
|Read( n )||Read ‘n’ from the End of this file|
|Readable()||When a file is readable, it provides a true value|
|Readline()||Read only one line of the file|
|Readlines()||Read all lines of the file|
|Close ()||Close the open file|
|Seekable()||When the file supports random access then it provides a true value|
|Tell()||Find out the current file location|
Hopefully, you enjoy this content on file handling in Python. It helps you to build up a web application that has several functions. Python file allows any kind of file open, read, write, and deleting process.
Have any questions feel free to drop a message below the comment box. I will be back as soon as possible.