Python Program to Write a List Content to a File

Files in Python:

Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

Given a file, the task is to write a list content into the given File.

Program to Write a List Content to a File in Python

Below is the full approach for writing a list content into the given File.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • 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.
  • Iterate in the above-given list using the for loop.
  • Write the iterator value(list elements) into the file using the write() function.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Read the above file using the read() function(get the content) and print it.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'Btechgeeks', 'good morning']
# 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.
with open(givenFilename, 'w') as givenfilecontent:
        # Iterate in the above given list using the for loop
        for itr in gvn_lst:
                # Write the iterator value(list elements) into the file using the write() function
                givenfilecontent.write("%s\n" % itr)

# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
filecontent = open("samplefile.txt")
# Read the above file using the read() function(get the content) and print it.
print(filecontent.read())

Output:

hello 
this
is 
Btechgeeks 
good morning

File Content (samplefile.txt):

hello 
this 
is 
Btechgeeks 
good morning

Method #2: Using For Loop (User Input)

Approach:

  • Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
  • 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.
  • Iterate in the above-given list using the for loop.
  • Write the iterator value(list elements) into the file using the write() function.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Read the above file using the read() function(get the content) and print it.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using the list(),map(),split(),
# int functions and store it in a variable.
gvn_lst = input("Enter some random list elements separated by spaces = ")
# 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.
with open(givenFilename, 'w') as givenfilecontent:
        # Iterate in the above given list using the for loop
        for itr in gvn_lst.split():
                # Write the iterator value into the file using the write() function
                givenfilecontent.write("%s\n" % itr)

# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
filecontent = open("samplefile.txt")
# Read the above file using the read() function(get the content) and print it.
print(filecontent.read())

Output:

Enter some random list elements separated by spaces = welcome to btechgeeks
welcome
to 
btechgeeks

File Content (samplefile.txt):

welcome
to 
btechgeeks

Google Colab Images:

Files and Code: