Python File Functions: Normal, CSV, and JSON
📝 Normal File Functions
open()
Syntax:
open(filename, mode)
Example:
f = open('[Link]', 'r')
read()
Syntax:
[Link]()
Example:
data = [Link]()
readline()
Syntax:
[Link]()
Example:
line = [Link]()
readlines()
Syntax:
[Link]()
Example:
lines = [Link]()
write()
Syntax:
[Link](data)
Example:
[Link]('Hello!')
writelines()
Syntax:
[Link](list)
Example:
[Link](['A\n', 'B\n'])
seek()
Syntax:
[Link](position)
Example:
[Link](0)
tell()
Syntax:
[Link]()
Example:
pos = [Link]()
📊 CSV File Functions
[Link]()
Syntax:
[Link](file)
Example:
import csv
with open('[Link]') as f:
reader = [Link](f)
[Link]()
Syntax:
[Link](file)
Example:
reader = [Link](f)
[Link]()
Syntax:
[Link](file)
Example:
writer = [Link](f)
[Link](['Name', 'Age'])
[Link]()
Syntax:
[Link](rows)
Example:
[Link]([['A', 21], ['B', 22]])
[Link]()
Syntax:
[Link](file, fieldnames=[])
Example:
writer = [Link](f, fieldnames=['name', 'age'])
[Link]()
Syntax:
[Link]()
Example:
[Link]()
🧩 JSON File Functions
[Link]()
Syntax:
[Link](file)
Example:
import json
with open('[Link]') as f:
data = [Link](f)
[Link]()
Syntax:
[Link](string)
Example:
data = [Link]('{"name": "A"}')
[Link]()
Syntax:
[Link](obj, file)
Example:
[Link](data, f)
[Link]()
Syntax:
[Link](obj)
Example:
s = [Link]({'name': 'A'})