Skip to content

Commit 3c36063

Browse files
committed
Create file_io.md
Some initial notes about interacting with files.
1 parent 0a38cca commit 3c36063

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

file_io.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# File Read Write
2+
(*copied from https://github.com/blutterfly/python/blob/main/docs/examples/file_io.md on 2025-10-21 - thank you 'butterfly'/Larry Prestosa*)
3+
4+
## File IO
5+
6+
Quick reference and examples on writing, saving, and reading files.
7+
Along with template functions for working with plain text, CSV, and JSON files.
8+
9+
---
10+
11+
## 1. Plain Text File
12+
13+
Plain text files are the simplest type of file. You can write strings to them or read their content as text.
14+
15+
### Template Function for Plain Text Files
16+
17+
Writing to a Plain Text File
18+
19+
```python
20+
def write_to_text_file(file_path, text):
21+
with open(file_path, 'w') as file: # Open file in write mode
22+
file.write(text) # Write text to the file
23+
```
24+
25+
Reading from a Plain Text File
26+
27+
```python
28+
def read_from_text_file(file_path):
29+
with open(file_path, 'r') as file: # Open file in read mode
30+
return file.read() # Return the content of the file
31+
```
32+
33+
Example Usage
34+
35+
```python
36+
write_to_text_file('example.txt', 'Hello, World!')
37+
content = read_from_text_file('example.txt')
38+
print(content)
39+
```
40+
41+
---
42+
43+
## 2. CSV File
44+
45+
CSV (Comma-Separated Values) files are used to store tabular data.
46+
47+
### Template Function for CSV Files
48+
49+
Writing to a CSV File
50+
51+
```python
52+
import csv
53+
54+
def write_to_csv(file_path, data):
55+
with open(file_path, 'w', newline='') as file: # Open file in write mode
56+
writer = csv.writer(file)
57+
writer.writerows(data) # Write rows of data
58+
```
59+
60+
Reading from a CSV File
61+
62+
```python
63+
def read_from_csv(file_path):
64+
with open(file_path, 'r') as file: # Open file in read mode
65+
reader = csv.reader(file)
66+
return list(reader) # Convert rows to a list
67+
```
68+
69+
Example Usage
70+
71+
```python
72+
data = [
73+
['Name', 'Age', 'Grade'],
74+
['Alice', 16, 'A'],
75+
['Bob', 17, 'B']
76+
]
77+
write_to_csv('students.csv', data)
78+
79+
content = read_from_csv('students.csv')
80+
for row in content:
81+
print(row)
82+
```
83+
84+
---
85+
86+
## 3. JSON File
87+
88+
JSON (JavaScript Object Notation) is used to store structured data, such as dictionaries or lists.
89+
90+
### Template Function for JSON Files
91+
92+
Writing to a JSON File.
93+
94+
```python
95+
import json
96+
97+
def write_to_json(file_path, data):
98+
with open(file_path, 'w') as file: # Open file in write mode
99+
json.dump(data, file, indent=4) # Write data to the file in JSON format
100+
```
101+
102+
Reading from a JSON File.
103+
104+
```python
105+
def read_from_json(file_path):
106+
with open(file_path, 'r') as file: # Open file in read mode
107+
return json.load(file) # Load data from JSON file
108+
```
109+
110+
Example Usage.
111+
112+
```python
113+
data = {
114+
'students': [
115+
{'name': 'Alice', 'age': 16, 'grade': 'A'},
116+
{'name': 'Bob', 'age': 17, 'grade': 'B'}
117+
]
118+
}
119+
write_to_json('students.json', data)
120+
121+
content = read_from_json('students.json')
122+
print(content)
123+
```
124+
125+
---
126+
127+
### Summary of File Types.
128+
129+
| File Type | Python Module | Best for |
130+
|-----------|---------------|------------------------------------|
131+
| Plain Text| `open()` | Storing simple text or logs |
132+
| CSV | `csv` | Tabular data |
133+
| JSON | `json` | Structured data (hierarchical) |
134+
135+
Each of these functions can be adapted based on specific requirements.
136+
137+
138+
### Other Related Reminders:
139+
* Python data I/O Cheat Sheet. [https://www.fabriziomusacchio.com/.../python_data_io](https://www.fabriziomusacchio.com/teaching/python_cheat_sheets/python_data_io)
140+
* Python File I/O Cheat Sheet. [https://www.cse.msu.edu/.../files-cheat-sheet.pdf](https://www.cse.msu.edu/~ldillon/cse-ctl/Spring2019/Meeting05/files-cheat-sheet.pdf)

0 commit comments

Comments
 (0)