FILE HANDLING IN PYTHON
Till now, we have made a lot of programs in Python and saved them. All these programs (source code) are saved into the computer with “.py” extension. But what if we want to store the output of the program? Let’s understand this by an example:
Suppose, a program is created for teachers which calculates the Total and Percentage after accepting Roll no, Name, and 5 subject marks from the teacher. Now, before Result day teacher enters all the details into this software. But on the very next day, i.e. on Result day whenever the teacher tries to Run this program, he will not find any existing record and result. Do you know why? What’s the problem in this scenario?
The problem is that the output added by the Teacher is not stored. If we can store the output of the teacher then the above problem will be resolved automatically.
Now, the next question is that here output is too big, which we are unable to store in a single variable, so where we can store this?
“FILE” is the solution for this problem that comes into the picture.
So, what is file?
- File helps in storing information permanently.
- A file in itself is a bunch of bytes stored on some storage device like hard-disk, thumb-drive etc.
- By means the information in the file remains even if the computer is switched off.
Types of Files :
Text Files in Python :
- Text file stores information in ASCII or UNICODE character. In text file everything will be stored as a character.
- In text file some translation takes place when this EOL character (‘\n’,’\r’) is read or written.
- In text file each line is terminated by special character called EOL.
- Text files are stored in human readable form and can be created using any text editor.
Binary Files in Python :
- It stores the information in the same format as in the memory i.e. data is store according to its data type so no translation occurs.
- If no translation is occurs then binary file are faster and easier for a program to read and write than are text file.
- In binary file there is no delimiter for a new line.
- Data in binary file cannot be directly read, it can be read only through python program for the same.
File Operations :
Now, it’s a time to operate a file. We can perform a variety of operations on file, however major operations are :
- Read
- Write (Add new content or Modifying existing file)
However, to perform both the operation, first, we need to open the file.
Open a File :
- In order to open a file we will make use of the open() function which is an inbuilt function. open() function creates a file object that serves as a link to a file residing on the computer.
- Syntax:
Let’s discuss the words that come in above syntax :
File Object in Python:
- open() function returned a file object which is also called file handle and this is the reason why this function is always called by a variable name (file object).
- File Object also called file-handle.
- File objects are used to read and write data to a file on disk.
- The file object is used to obtain a reference to the file on disk and open it for a number of different task.
- It is very important and useful tool as through file-object only, a Python program can work with files stored on hardware.
- All the functions that are performed on a data file are performed through file-objects.
- A file object is a stream of bytes where the data can be read either byte by byte or line by line.
Parameters of Open() :
<filename> : first parameter is a file name which we want to open. However, if we give only file name then it will search the file on current directory.
<mode> : it opens the file as per the mode i.e. “r” for read, “w” for write, “a” for append. If second parameter is not given then by default it opens the file in read mode.
Closing File :
A close() function is also built in function which breaks the link of file-object and the file on the disk. After close(), no tasks can be performed on that file through the file-object.
CREATING TEXT FILE MANUALY (Without help of Python or Program)
Let’s understand how to create and store a text file in computer? To create a text file, follow the following steps :
STEP 1 : Create a folder in “D:\” or “E:\” drive named “File_Programs”.
STEP 2 : Inside the folder, right click and click “New” then click on “Text Document” as shown in below picture.
STEP 3: Rename this “New Text Document.txt” to any file name, in this case we provide “About.txt” file, like
STEP 4: Now, put some data inside it and as per example our content is :
Conclusion
In conclusion, understanding file handling in Python is essential for any programmer aiming to manipulate data effectively. Files serve as the backbone for storing and retrieving information, and a comprehensive grasp of their types and operations is crucial. In this article, we delved into the concept of files, exploring their various types such as text files and binary files. We also touched upon file operations and the significance of file objects in Python. As we navigate through the diverse landscape of file handling, it’s important to stay tuned for the next installment of our series. In the upcoming article, we will dive into practical aspects, learning how to perform read and write operations on files in Python. We will explore different modes of file handling, empowering you with the knowledge to seamlessly manipulate data within your programs.
FAQ’s on File Handling in Python :
What is a file in Python?
In Python, a file is a named location on disk used to store information. It can be a text file containing readable characters or a binary file storing data in a format that is not human-readable.
What are the types of files in Python?
There are two main types of files in Python: text files and binary files. Text files store data in plain text format, while binary files store data in a more compact, non-human-readable form.
How are text files different from binary files?
Text files contain human-readable characters, often organized in lines of text, making them editable using a simple text editor. On the other hand, binary files store data in a more compact, machine-readable format and may contain a variety of data types.
What are some common file operations in Python?
Common file operations in Python include opening, reading, writing, and closing files. These operations allow programmers to interact with file contents, modify data, or create new files.
What is the significance of File Objects in Python?
File objects are Python’s representation of a file on the disk. They provide methods and attributes for performing various file operations. File objects are created when a file is opened using the `open()` function in Python.
How can I open a file in Python?
You can open a file in Python using the `open()` function, specifying the file path and the mode (read, write, append, etc.). For example, `open(“example.txt”, “r”)` opens the file “example.txt” in read mode.
Can I work with both text and binary data in the same Python program?
Yes, Python allows you to work with both text and binary data in the same program. You can use different modes when opening files to specify whether you want to work with text or binary data.