File Handling in Python | Learn Python

Any programming language must include file management in order for programs to communicate with files stored on a computer's storage system. Python, like other programming languages, provides built-in functions and modules for file handling. With these functions and modules, you can create, read, write, update, and delete files on your computer. As with other notions in the Python language, the idea of file management has been extended across numerous computer languages, but the implementation is either time-consuming or difficult. also easy & short.



File handling in Python has very limited modes like read, write & append modes and each mode has very limited functions like ‘.write()’, ‘.read()’, and so on. Let’s dive into it.

Let’s start with a basic understanding of the file-handling concept. It has been briefly told in the very first paragraph, where is its real-life usage. We can use it in making an application or software in which you have to keep track of every single step of the process like the machine at the entrance of every major company’s building in which an employee scans their thumb and its attendance is marked and the person who has the access to that machine can know when the employee entered the building and when it left.

Let me give you an example of how creating a basic file with your name in it looks like: -


This will create a .txt file in computer storage and this is the output:-



Now in this code ‘file’ variable is the reserved space in the memory to hold the mentioned file temporarily for conducting specified operations. 

Python's open() function is used to open a file. The name of the file to be opened and the mode in which it should be opened are the two arguments that the open() function accepts. “r” stands for reading, “w” for writing, or “a” for attaching, respectively, as the mode. Once a file has been opened,

it can be read, written to, or appended using various methods. There are several access modes available in Python:
  • "r" - Read Mode: This mode is used to open a file for reading. If the file does not exist, an error is raised and the file reference is put at the start of the file.
  • "r+" – Read and Write Mode: This mode is used to open a file for reading and writing.
  • "rb" - Read Binary Mode: This mode is used to open a file in binary mode for reading.
  • "rb+" – Read and Write Binary Mode: This mode is used to open a file in binary mode for reading and writing.
  • "w" - Write Mode: This mode is used to open a file for writing. If the file exists, its contents are truncated, and if the file does not exist, a new file is created.
  • "w+" – Write and Read Mode: This mode is used to open a file for writing and reading.
  • "wb" - Write Binary Mode: This mode is used to open a file in binary mode for writing.
  • "wb+" – Write and Read Binary Mode: This mode is used to open a file in binary mode for writing and reading.
  • "a" - Append Mode: This mode is used to open a file for writing. The file pointer is placed at the end of the file, the content of the file will remain intact and the new text will add at the end of the file but if the file does not exist, a new file is created.
  • "a+" – Append and Read Mode: This mode is used to open a file for appending and reading.
  • "ab" - Append Binary Mode: This mode is used to open a file in binary mode for appending.
  • "ab+" – Append and Write Binary Mode: This mode is used to open a file in binary mode for appending and reading.

There are many functions in each and every access modes mentioned above. Now let’s get to know what this function does.

Write mode

  • write()- This function is used to write a string to the file. It writes a string to the file and accepts a string as input. If the file is not opened in write mode, this function will raise an error.
  • writelines()- This function is used to write a list of strings to the file. It writes strings to the file using a list of strings as a parameter. If the file is not opened in write mode, this function will raise an error.

Output:-


  • truncate() - This function is used to truncate the file to a specific size. It takes an optional size argument, which represents the new size of the file. If no size is provided, it truncates the file to the current position of the file pointer.

Output:-



#Note

These functions also work in append mode.

Read Mode

  • read() - This function is used to read the entire content of the file as a string. If no argument is given, the full file is read.. If an argument is provided, it reads that many characters from the file.

output:-



  • readline() - This function is used to read a single line from the file. Each time this function is called, it reads the next line in the file. It produces an empty string if the file has reached its conclusion.

output:-

  • readlines()- This function is used to read all the lines of the file as a list of strings. Each string in the list represents a single line from the file.

output:-



File Pointer

In Python file handling, the file pointer is a marker that points to a specific position in a file, indicating the location in the file where the next read or write operation will take place. The file pointer starts at the beginning of the file when the file is opened, and it moves as data is read from or written to the file.

The position of the file pointer can be changed using the seek() function. This function takes two arguments: an offset value and a reference point. The reference point can be 0, 1, or 2, which represent the beginning of the file, the current position of the file pointer, and the end of the file, respectively. The number of bytes to shift the file pointer is specified by the offset value.

Here are some examples of using the seek() function to change the position of the file pointer:


In this example, we first opened the file in read mode and read the first line. Then we used the seek() function to move the file pointer to the beginning of the file and read the entire file from the beginning. Next, we used the seek() function again to move the file pointer to the end of the file and write some text to the end of the file. Finally, we closed the file.

It's important to note that the position of the file pointer affects the behavior of the read and write functions. For example, if the file pointer is at the end of the file and you call the read() function, it will return an empty string. If you call the write() function, it will append the data to the end of the file.


Functions of file pointer

  • tell()- This function gives the current file pointer position in the file, expressed as the number of bytes from the file's start.

output:-


  • seek() - This function is used to move the file pointer to a specific position in the file. It takes an offset value and a reference point as arguments. The reference point can be 0, 1, or 2, which represent the beginning of the file, the current position of the file pointer, and the end of the file, respectively.

output:-


  • readable() and writable()- These functions are used to check if the file can be read or written, respectively.

output:-


  • closed() - This attribute is used to check if the file is closed or not.

output:-




Conclusion

In conclusion, Python file handling provides a convenient and powerful way to work with files. With Python's built-in functions and modules, you can easily open, read, write, and manipulate files in a variety of formats.

In this article, we covered the basics of file handling in Python, including the different access modes available, the functions available in write and read mode, and the file pointer and its associated functions.

By using Python's file-handling capabilities, you can automate a wide range of tasks that involve reading and writing data to files. Whether you need to process log files, parse data from CSV files, or generate reports in PDF format, Python's file-handling capabilities make it easy to work with files in your code.


so that's it for today...follow us for learning more...

No comments:

Post a Comment