File Input Output¶
Text files¶
Text files are a simple way to save and retrieve data. They have the advantage of being human readable, so you can open a text file and see what has been saved.
The disadvantage of text files is that everything is save and read as strings. You will need to String Methods and data type converters to change the data back to the format you wish to use.
Reading text files¶
Read all the file¶
1with open(r"python_files\names.txt", "r", encoding="utf-8") as file:
2 data = file.read()
3
4print(data)
Note:
"python_files\names.txt"
→ path to the text file"r"
→ read modeall values read from text files are strings
Read one line¶
1with open(r"python_files\names.txt", "r", encoding="utf-8") as file:
2 data = file.readline()
3
4print(data)
Note:
"python_files\names.txt"
→ path to the text file"r"
→ read modeall values read from text files are strings
Read a set number of lines¶
1with open(r"python_files\names.txt", "r", encoding="utf-8") as file:
2 data = file.readlines(5)
3
4print(data)
Note:
"python_files\names.txt"
→ path to the text file"r"
→ read modeall values read from text files are strings
Read the file into a list¶
1with open(r"python_files\names.txt", "r", encoding="utf-8") as file:
2 data = file.read().split("\n")
3
4print(data)
Note:
"python_files\names.txt"
→ path to the text file"r"
→ read modeall values read from text files are strings
Writing text files¶
Writing to a text file will:
create a new text file if it doesn’t already exist
overwrite the text file if it does already exist
Write a variable to text file¶
1value = "String that will be written to text file"
2
3with open("python_files\save_variable.txt", "w") as file:
4 file.write(value)
Note:
"python_files\save_file.txt"
→ path to the text file"w"
→ write modeall values written to text files are strings
Write a list to text file¶
This will write each list element on a new line.
1values = ["Michelle", "Nicole", "Emma", "Justine", "Maria"]
2
3with open("python_files\save_list.txt", "w") as file:
4 for item in values:
5 file.write(f"{item}\n")
Note:
"python_files\save_file.txt"
→ path to the text file"w"
→ write modeall values written to text files are strings
Pickle files¶
Python’s pickle module saves Python objects as binary files. This may seem limiting but remember that everything in Python is an object.
The advantage of using pickle to save is the object type is preserved. For example, if you save a dictionary, it will return as a dictionary when you load it.
The disadvantage of pickling is that the save file is in binary, so it is not human readable. You cannot open the file to see what has been saved.
Save a file (pickling)¶
1import pickle
2
3values = ["Michelle", "Nicole", "Emma", "Justine", "Maria"]
4
5with open("python_files/save_file.pkl", "wb") as file:
6 pickle.dump(values, file)
Note:
"python_files/save_file.pkl"
→ the path to the pickle filewb
→ the file needs to write binary
Read a file (unpickle)¶
1import pickle
2
3with open("python_files\save_file.pkl", "rb") as file:
4 values = pickle.load(file)
5
6print(values)
Note:
"python_files/save_file.pkl"
→ the path to the pickle filerb
→ the file needs to read binary