Open In App

Write Multiple Variables to a File using Python

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Storing multiple variables in a file is a common task in programming, especially when dealing with data persistence or configuration settings. In this article, we will explore three different approaches to efficiently writing multiple variables in a file using Python.

Write Multiple Variables To A File in Python

Below are the possible approaches to writing multiple variables to a file in Python.

Write Multiple Variables To A File Using the repr() function

In this approach we are using the repr() function to convert each variable into its string representation, preserving its data type, and write them to a file named ‘output.txt‘ in the format of variable assignments, allowing for easy reconstruction of the variables when read back from the file.

Python3




# data
website_name = "GeeksforGeeks"
founded_year = 2009
is_popular = True
 
# open a file in write mode
with open('output.txt', 'w') as file:
    # write variables using repr() function
    file.write("website_name = " + repr(website_name) + '\n')
    file.write("founded_year = " + repr(founded_year) + '\n')
    file.write("is_popular = " + repr(is_popular) + '\n')


Output:

output.txt
website_name = 'GeeksforGeeks'
founded_year = 2009
is_popular = True

Write Multiple Variables To A File Using the string formatting

In this approach, we are using string formatting to write multiple variables (website_name, founded_year, and is_popular) to a file named ‘output.txt’, creating a clear and human-readable representation of the data with formatted labels and values.

Python3




# data/variables
website_name = "GeeksforGeeks"
founded_year = 2009
is_popular = True
 
# open a file in write mode
with open('output.txt', 'w') as file:
    # write variables using string formatting
    file.write("Website Name: {}\n".format(website_name))
    file.write("Founded Year: {}\n".format(founded_year))
    file.write("Is Popular: {}\n".format(is_popular))


Output:

output.txt
website_name = 'GeeksforGeeks'
founded_year = 2009
is_popular = True

Write Multiple Variables To A File Using the str() function

In this approach, we are using the str() function to convert each variable (website_name, founded_year, and is_popular) into strings and writes them to a file named ‘output.txt’, presenting a proper representation of the variables with labeled values.

Python3




# data
website_name = "GeeksforGeeks"
founded_year = 2009
is_popular = True
 
# open a file in write mode
with open('output.txt', 'w') as file:
    # write variables using str() function
    file.write("Website Name: " + str(website_name) + '\n')
    file.write("Founded Year: " + str(founded_year) + '\n')
    file.write("Is Popular: " + str(is_popular) + '\n')


Output:

output.txt
website_name = 'GeeksforGeeks'
founded_year = 2009
is_popular = True



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads