Python can work with various file types, from plain text to complex binary formats. Understanding file types helps you choose the right file mode and libraries for processing them efficiently.
'r'
, 'w'
, or 'a'
.'rb'
or 'wb'
.csv
module.json
module.configparser
or pyyaml
.Pillow
or open in binary mode.PyPDF2
or python-docx
.openpyxl
or pandas
to read and write.BeautifulSoup
from bs4
to parse HTML content.with open('notes.txt', 'r') as file:
print(file.read())
import csv
with open('data.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Score'])
writer.writerow(['John', 90])
import json
with open('config.json', 'r') as file:
data = json.load(file)
print(data['username'])
import json
data = {'username': 'sai', 'score': 100}
with open('output.json', 'w') as file:
json.dump(data, file)
with open('photo.jpg', 'rb') as file:
data = file.read()
print(len(data), "bytes read")
with open('app.log', 'a') as file:
file.write('New entry added\\n')
import configparser
config = configparser.ConfigParser()
config.read('settings.ini')
print(config['DEFAULT']['theme'])
from PIL import Image
img = Image.open('image.png')
print(img.format, img.size)
import PyPDF2
with open('doc.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
print(reader.pages[0].extract_text())
from docx import Document
doc = Document('file.docx')
for para in doc.paragraphs:
print(para.text)
import pandas as pd
df = pd.read_excel('data.xlsx')
print(df.head())
from bs4 import BeautifulSoup
with open('example.html', 'r') as file:
soup = BeautifulSoup(file, 'html.parser')
print(soup.title.string)