Python Practice › File Handling Programs › Program 22
Write a tuple to a file
Write a Python program to write a tuple to a file and read it back.
Output
(12, 45, 78) True
Approach
- Write one item per line; convert back when reading.
Solution
data = (12, 45, 78)with open("tuple.txt", "w") as f: f.write("\n".join(map(str, data))) with open("tuple.txt") as f: loaded = tuple(int(line) for line in f)print(loaded, loaded == data)Output
(12, 45, 78) True
Learn the concept
New to this topic? Read the lesson, then come back.