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,
- "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.
- 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.
#Note
These functions also work in append 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.
- 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.
- 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:-
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.
- 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