Python File write() Method with Examples

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 write() Method in Python:

The write() function is a built-in Python method that is used to write a specified text to a file.

In which the specified text will be inserted depending on the file mode and stream position.

“a”: The text is added at the current file stream point, which is usually at the end of the file(by default).

“w”: The file will be emptied before the text is inserted at the current file stream position, which is 0 by default.

Syntax:

fileobject.write(text/bytes)

Parameters

text/bytes: It is the text or byte object to be inserted.

Return Value:

This method’s return type is <class ‘int’>, and it returns the total number of lines written in the file.

File write() Method with Examples in Python

Example

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 append mode. In this case, we’re simply appending/adding the contents into the file.
  • Write some random text into the file using the write() function.
  • Close the given file using the close() function.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Read the contents of the file using the read() function and print it.
  • The Exit of 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 append mode. In this case, we're simply appending/adding the contents into the file.
gvn_file = open(givenFilename, 'w') 
# Write some random text into the file using the write() function
gvn_file.write("hello this is btechgeeks")
# Close the given file using the close() function
gvn_file.close()

# Open the file in read mode. In this case, we're simply reading the contents of the file.
read_file = open(givenFilename, 'r') 
# Read the contents of the file using the read() function and print it
print(read_file.read())

Output:

hello this is btechgeeks

File Content:

hello this is btechgeeks

Google Colab Images:

Files and Code: