In python, we have discussed many concepts and conversions. But sometimes, we come to a situation where we need to read a text from a file, write a text from a file, append a text in a file, and save a file in python. In this tutorial, we will be discussing how to save a variable in a file.
What are file access modes?
Access modes are used to govern the type of operations possible in the opened file. It tells us about how the file will be used once it’s opened. These modes define the location of the file handle in the file. The file handle is just like a cursor, which tells from where the data has to be read or written in the file. We have 6 modes in python:
- Read-only (‘r’): It only opens text files for reading. If the file does not exist, it raises the I\O error. This is the default mode in which the file is opened.
- Write only (‘w’): It opens the file for writing. If the file exists previously, the data is truncated and over-write the data. it creates the file if the filename does not exist.
- Read and Write (‘r+’): It opens the file for reading and writing. It raises an I/O error if the file does not exist.
- Write and read (‘w+’): It opens the file for reading and writing. If the file exists, the data is over-written.
- Append only (‘a’): It opens the file for writing. If the file does not exist, it gets created. The data which is written gets appended at the last of the data which is already written.
- Append and read (‘a+’): It opend=s a file for reading and writing. The file gets created if does not exist. The data which is being written is inserted at the end of the data, after the existing data.
Many times a user or a python programmer needs to delete or rename a file. To know how to execute these tasks you can read our posts from here:
Ways to save a variable in a file in python
Here, we will be discussing all the different ways through which we can save a variable in a file in python:
1. Using string concatenation to save a variable in a file in Python
In this example, we will use open(file, mode) with the file’s pathname as files and mode as ‘w’ to open the file for writing. Then, we will use repr(object) with the object as the variable to convert a variable to a string. After that, we will call the file.write(data) with data as a string concatenation of three strings, a string containing the variable name and =
the string version of the variable, and "\n"
. At last, we will use a file.close() to close the file. Let us look at the example for understanding the concept in detail.
#input text
input_dictionary = {"one" : 1, "two" : 2}
#open file
file = open("Python.txt", "w")
#convert variable to string
str = repr(input_dictionary)
file.write("input_dictionary = " + str + "\n")
#close file
file.close()
f = open('Python.txt', 'r')
if f.mode=='r':
contents= f.read()
Output:
Explanation:
- Firstly, Then, we will take an input dictionary with two values in it.
- Then, we will open the file with file = open(). We have passed the file name and ‘w’ as the write mode as the parameter.
- Then, we will convert the variable into the string with the repr() function.
- Then, we will apply file.write() in which we have passed the input dictionary.
- At last, we will be closing the file with file.close().
- Hence, you can see the data gets saved in the particular file.
2. Using String formatting to save a variable in a file in Python
In this example, we will be using open(file, mode) with the file’s pathname as a file and modes as ‘w’ to open the file for writing. Then, we will call the file.write(data) with data as the string formats “%s %d” followed by % and a tuple containing a string of the variable name and the variable. At last, we will close the file by using file.close(). Let us look at the example for understanding the concept in detail.
#input variable
input_dictionary = {"one" : 1, "two" : 2}
file = open("Python.txt", "w")
file.write("%s = %s\n" %("input_dictionary", input_dictionary))
file.close()
f = open('Python.txt', 'r')
if f.mode=='r':
contents= f.read()
Output:
Explanation:
- Firstly, Then, we will be taking an input dictionary with two values in it.
- Then, we will open the file with file = open(). Inside which we have passed the file name and ‘w’ as the write mode as the parameter.
- Then, we will apply file.write() in which we have passed the input dictionary.
- At last, we will be closing the file with file.close().
- Hence, you can see the data gets saved in the particular file.
3. By importing pickle library to save a variable in a file
In this example, we will be importing the pickle library. Then, we will be using open(file, mode) with the pathname of a file as a file and modes as ‘w’ to open the file for writing. After that, we will apply the pickle library with the parameters as dict and file as a variable. At last, we will close the file by using file.close(). Let us look at the example for understanding the concept in detail.
import pickle
input_dictionary = {'one': 1, 'two': 2}
file = open('Python.txt', 'w')
pickle.dump(input_dictionary, file)
file.close()
with open('Python.txt', 'rb') as f:
dict = pickle.load(f)
Output:
Explanation:
- Firstly, we will be importing the pickle module.
- Then, we will be taking an input dictionary with two values in it.
- Then, we will open the file with file = open(). We have passed the file name and ‘w’ as the write mode as the parameter.
- After that, we will apply the dump() function from the pickle module, which will dump all the data into the particular file.
- At last, we will close the file with file.close().
- Hence, you can see the output.
4. Using numpy library to save a variable in a file
In this example, we will be importing a numpy library with an alias name as np. Then we will be taking two lists: a and b. Then, we will apply the savetxt() function from the numpy library. Let us look at the example for understanding the concept in detail.
import numpy as np
a = [1,2,3,4,5]
np.savetxt('Python.txt',a)
print(open("E:\python.txt").read())
Output:
Explanation:
- Firstly, we will be importing the numpy module with an alias name as np.
- Then, we will be taking input as a list in the variable a.
- Then, we will apply the savetxt() function from the numpy library.
- Inside which we have passed the file name and the list as the parameter.
- At last, if we open the same file, we will see the output as shown.
- Hence, you can see the output is saved in the given file.
Conclusion
In this tutorial, we have learned about the concept of saving a variable in a file in python. We have seen all the access modes in the file. After that, we have discussed all the ways through which we can save a variable in a file in python. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Please don't do that, even as an example. dict is a built-in name, of an important data type, and now you've made its original value inaccessible.
You wouldn't do that with print, int, str, open, ... would you?
Hi,
Yes, that’s a great catch. Using dict as a variable is not favorable since it’s a built-in name.
I’ve changed the variable names from dict to input_dictionary to avoid this error.
Regards,
Pratik