Python write list to file with examples

In this Python tutorial, we will discuss how to write list to file in Python. We will also check:

  • Python write list to file with newline
  • Python write list to file and read
  • Python write list to file without brackets
  • python write list to file with comma-separated
  • Python write list to file with space
  • python write list to file as CSV

Python write list to file

Let us start with an example, how to write list to file in Python.

Example:

mobile=['samsung','redmi','oneplus']

file=open('f1.txt','w')
for items in mobile:
    file.writelines([items])

file.close()

Here we can see how to write list to file in Python.

In this example, I have taken a list as mobile and assigned some items to it and to write the items of a Python list in a file, I am using file.writelines().

You may like Draw colored filled shapes using Python Turtle.

Python write list to file with newline

Now we can see, how to write a list to a file with newline in python.

In this example, I have taken a Python list of items and assigned them to a list mobile. and file=open(‘filename.txt’, mode) to open a file.

for loop is used to iterate over a sequence file.write lines() is used to write a list to file,“\n” is used to write a list of items in the new line, file.close() to close the file.

Example

mobile=['samsung','redmi','oneplus']
file=open('f1.txt','w')
for items in mobile:
    file.writelines(items+'\n')
file.close()

Below image shows the list of items in the file:

Python write list to file
Python write list to file

Python write list to file without brackets

Here we can see how to write list to file without brackets in python.

In this example, we can see how to conversion of mobile = [“samsung”, “redmi” ,”oneplus”] to Samsung, redmi, oneplus and * is used to unpack the list and sep = ” , “ is used to separate the list

Example

mobile = ["samsung","redmi","oneplus"]
sourceFile = open('f2.txt', 'w')
print(*mobile, sep = ",", file = sourceFile)
sourceFile.close()

Below image shows the output:

Python write list to file without brackets
Python write list to file without brackets

Python write list to file with comma-separated

Now we can how to write list to file with comma separated in Python.

In this example, we can see that a list as mobile containing some items in it. Here I used “, ” .join (string)to join the list. now we can see each item is separated by a comma.

Example

mobile = ["samsung",   "redmi",   "oneplus"]
sourceFile = open('f3.txt', 'w')
joined_list = ",".join(mobile)
print(joined_list , file = sourceFile)
sourceFile.close()

Below image shows the output:

Python write list to file with comma separated
Python write list to file with comma separated

Python write list to file with space

now we can see how to write list to file with space in Python by taking an example of converting mobile = [“samsung”, “redmi”, “oneplus”] to samsung redmi oneplus.

In this example, I am making a list as mobile to write a list to file with space ” “.join() is used to get space between each item in the list.

Example

mobile = ["samsung","redmi","oneplus"]
sourceFile = open('f3.txt', 'w')
joined_list = "  ".join(mobile)
print(joined_list , file = sourceFile)
sourceFile.close()

In this below image you can the out as items with space.

Python write list to file with space
Python write list to file with space

Python write list to file as csv

A CSV file is a comma-separated value file, It is a plain text file which uses some structure to arrange the items of lists in tabular format.

Here in this example, I have imported CSV module and assigned a list as mobile, To open the file, I have used open(‘filename.csv’, ‘mode’, newline = ‘ ‘) newline = ‘ ‘ is used to add a new line of text or to add space between lines. writerows() to write the items in row format.

Example:

import csv 
mobile = [['samsung'], [5], ['oneplus !']] 
file = open('f6.csv', 'w+', newline ='') 
with file:     
    write = csv.writer(file) 
    write.writerows(mobile) 
file.close()

Below example shows the output:

Python write list to file as csv
Python write list to file as csv

You may like the following Python tutorials:

In this tutorial, we learned how to write python list to file.

  • Python write list to file
  • Python write list to file with newline
  • Python write list to file without brackets
  • Python write list to file with comma-separated
  • Python write list to file with space
  • Python write list to file as csv