Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Reading and Writing lists to a file in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Reading and writing files is an important functionality in every programming language. Almost every application involves writing and reading operations to and from a file. To enable the reading and writing of files programming languages provide File I/O libraries with inbuilt methods that allow the creation, updation as well as reading of data from the files. Python is no exception. Python too offers inbuilt methods to perform file operations. The io module in Python is used for file handling.

The following examples demonstrate reading and writing lists to a file in Python

The open(filepath, mode) is used to open the required file in the desired mode. The open() method supports various modes of which three are of main concern:

  • r:  read (default)Python
  • w: write
  • a: append

write(): Inserts the string str1 in a single line in the text file.

read(): used to read data from the file opened using the open() method. 

Reading files in Python

Method 1: Read a Text file In Python using read()

The file is opened using the open() method in reading r mode. The data read from the file is printed to the output screen using read() function. The file opened is closed using the close() method.

Python3




# open file in read mode
f = open('gfg.txt', 'r')
 
# display content of the file
print(f.read())
 
# close the file
f.close()


Output:

 

Method 2: Read a Text file In Python using readlines()

The file is opened using the open() method in reading r mode. The data read from the file is printed to the output screen using readlines() function. The file opened is closed using the close() method.

Python3




# open file in read mode
f = open('gfg.txt', 'r')
 
# display content of the file
for x in f.readlines():
    print(x, end='')
 
# close the file
f.close()


Output:

 

Writing files in Python

Method 1: Write in a Text file In Python using write()

The file is opened with the open() method in w+ mode within the with block, the w+ argument will create a new text file in write mode with the help of write(). The with block ensures that once the entire block is executed the file is closed automatically.

Python3




# assign list
l = ['Geeks','for','Geeks!']
 
# open file
with open('gfg.txt', 'w+') as f:
     
    # write elements of list
    for items in l:
        f.write('%s\n' %items)
     
    print("File written successfully")
 
 
# close the file
f.close()


Output:

File written successfully

Here is the text file gfg.txt created:

 

Method 2:  write in a Text file In Python using writelines()

The file is opened with the open() method in w mode within the with block, the argument will write text to an existing text file with the help of readlines(). The with block ensures that once the entire block is executed the file is closed automatically.

Python3




L = ["Geeks\n", "for\n", "Geeks\n"]
 
# writing to file
file1 = open('test1/myfile.txt', 'w')
file1.writelines(L)
file1.close()


Output:

Below is the text file gfg.txt:

 


My Personal Notes arrow_drop_up
Last Updated : 02 Aug, 2022
Like Article
Save Article
Similar Reads
Related Tutorials