Responsive Navbar with Google Search
☰ Menu
🏠 Home
Python
📄 LaTeX
📊 GNUPlot
💬 Feedback
✉️ Contact
Introduction to Python: I/O Operation
Introduction to Python
Print Function
Variable and Data Types
Mathematical Operations
Conditionals (if, elif, else)
For Loop
While Loop
User Defined Function
Module math and cmath
I/O Operation
Loading Python Environment…
Python runs in your browser. Heavy or infinite loops may freeze your browser tab. Use "Stop" if needed; for heavy jobs, run locally.
Program 1
I/O Operation
'''write text to file''' with open("example.txt", "w") as file: file.write("Hello, User! This is a file write example.") '''read text from file''' with open("example.txt", "r") as file: content = file.read() print(content) '''Append text to file''' with open("example.txt", "a") as file: file.write("\nThis is an appended line.") '''read file after append''' with open("example.txt", "r") as file: content = file.read() print(content)
Run
Stop
Output 1
Program 2
I/O Operation
# Writing numbers to a file numbers = [1, 2, 3, 4, 5,6,7,8] with open('data.txt', 'w') as file: for number in numbers: file.write(str(number) + "\n") # Reading numbers from the file with open('data.txt', 'r') as file: content = [int(line.strip()) for line in file] print(content)
Run
Stop
Output 2
Program 3
I/O Operation
# Writing structured data to a file data = [["ID", "Name", "Score"], [1, "ABC", 85], [2, "DEF", 90], [3, "GHI", 78]] with open('data.txt', 'w') as file: for row in data: file.write(" | ".join(map(str, row)) + "\n") # Reading structured data from the file with open('data.txt', 'r') as file: content = file.read() print(content)
Run
Stop
Output 3