24
loading...
This website collects cookies to deliver better user experience
open()
method and pass a file path to the function.write()
, writelines()
.close()
function.open()
function opens the file if possible and returns the corresponding file object.open()
function has a lot of parameters. Let’s take a look at the necessary params for writing to a text file. It opens the file in a specified mode and returns a file object.mode
is an optional parameter. It’s a string that specifies the mode in which you want to open the file.Mode | Description |
---|---|
'r' |
Open a file for read mode (default if mode is not specified) |
'w' |
Open a file for writing. Python will create a new file if does not exist or truncates a file content if file exists |
'x' |
Open a file for exclusive creation. |
'a' |
Open a file for appending the text. Creates a new file if file does not exist. |
't' |
Open a file in text mode. (default) |
'b' |
Open a file in binary mode. |
'+' |
Open a file for updating (reading and writing) |
file = open('C:\hello.txt','w')
write()
: *The *write()
function will write a line to a text file. It inserts a single line in the text file.writelines()
: The writelines()
*function will write multiple string lines at once to a text file. The *writelines()
method accepts an iterable object such as *list, set, tuple, etc. *
close()
function. It is a must and best practice to perform this operation after writing the data into the file as it frees up the memory space acquired by that file. Otherwise, it may cause an unhandled exception. write()
*method. We will use the *with
statement, which helps to close the file once the write operation is performed. We don’t have to specify any explicit close method.# Program to write to text file using write() function
with open("python.txt", "w") as file:
content = "Hello, Welcome to Python Tutorial !! \n"
file.write(content)
file.close()
# Program to read the entire file (absolute path) using read() function
with open("C:/Projects/Tryouts/python.txt", "r") as file:
content = file.read()
print(content)
file.close()
Hello, Welcome to Python Tutorial !!
write()
operation, as shown below.# Program to append to text file using write() function
with open("python.txt", "a") as file:
content = "Append the content at the end \n"
file.write(content)
file.close()
# Program to read the entire file (absolute path) using read() function
with open("C:/Projects/Tryouts/python.txt", "r") as file:
content = file.read()
print(content)
file.close()
Hello, Welcome to Python Tutorial !!
Append the content at the end
writelines()
*method. The *writelines()
method accepts an iterable object such as list, set, tuple, etc. In the below example let’s see how to write a list to a file in Python# Program to write multiple lines to text file using writelines() function
with open("python.txt", "w") as file:
content = ["Hello\n", "Welcome to Python Tutorial\n", "Cheers \n"]
file.writelines(content)
file.close()
# Program to read the entire file (absolute path) using read() function
with open("C:/Projects/Tryouts/python.txt", "r") as file:
content = file.read()
print(content)
file.close()
Hello
Welcome to Python Tutorial
Cheers
writelines()
operation, as shown below.# Program to append to text file using writelines() function
with open("python.txt", "a") as file:
content = ["Appending the content\n", "Python\n"]
file.writelines(content)
file.close()
# Program to read the entire file (absolute path) using read() function
with open("C:/Projects/Tryouts/python.txt", "r") as file:
content = file.read()
print(content)
file.close()
Hello
Welcome to Python Tutorial
Cheers
Appending the content
Python