Files in Python:
One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.
Also, if you didn’t know, I/O operations are the most expensive processes where a programme can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.
Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.
File errors Property in Python:
In Python, the errors Property is a built-in property of the File object (IO object), and it is used to obtain the Unicode error handler along with the Unicode.
Syntax:
fileobject.errors
Parameters: This property has no arguments
Return Value: This method’s return type is <class’str’>, and it returns the Unicode error handler as a string.
- Python Program to Count Number of Digits in a Text File
- How to run Python scripts
- Python Interview Questions on File Manipulation
File errors Property with Examples in Python
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in write mode. In this case, we’re writing the contents into the file.
- Apply errors function to the given file and print it.
- Close the given file using the close function.
- Open the file in append mode. In this case, we’re appending the contents into the file.
- Apply errors function to the given file and print it.
- Close the given file using the close function.
- Open the file in write mode and pass errors =’ignore’ as the arguments to the open() function.
- Apply errors function to the given file and print it.
- Close the given file using the close function.
- The Exit of the Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in write mode. In this case, we're writing the contents into the file. gvn_file = open(givenFilename, 'w') # Apply errors function to the given file and print it print("In write-mode:", gvn_file.errors) # Close the given file using the close function gvn_file.close() # Open the file in append mode. In this case, we're appending the contents into the file. gvn_file = open(givenFilename, 'a') # Apply errors function to the given file and print it print("In append-mode:", gvn_file.errors) # Close the given file using the close function gvn_file.close() # Open the file in write mode and pass errors ='ignore' as the # arguments to the open() function gvn_file = open(givenFilename, 'w', errors='ignore') # Apply errors function to the given file and print it print("In write-mode with errors=ignore:", gvn_file.errors) # Close the given file using the close function gvn_file.close()
Output:
In write-mode: strict In append-mode: strict In write-mode with errors=ignore: ignore
File Content:
Hello this is btechgeeks Good morning btechgeeks welcome to python-coding platform
Google Colab Images:
Files and Code: