Skip to content

PRO TIP Block youtube.com at the DNS level — Pi-hole, NextDNS or your hosts file — but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

Read files using Python! 🔍

44.4K views on YouTube

# Python reading files (.txt, .json, .csv)

# ———- .txt ———-

file_path = “C:/Users/HP/Desktop/input.txt”

try:
with open(file_path, ‘r’) as file:
content = file.read()
print(content)
except FileNotFoundError:
print(“That file was not found”)
except PermissionError:
print(“You do not have permission to read that file”)

# ———- .json ———-
import json

file_path = “C:/Users/HP/Desktop/input.json”

try:
with open(file_path, ‘r’) as file:
content = json.load(file)
print(content )
except FileNotFoundError:
print(“That file was not found”)
except PermissionError:
print(“You do not have permission to read that file”)

# ———- .csv ———-
import csv

file_path = “C:/Users/HP/Desktop/input.csv”

try:
with open(file_path, ‘r’) as file:
content = csv.reader(file)
for line in content:
print(line)
except FileNotFoundError:
print(“That file was not found”)
except PermissionError:
print(“You do not have permission to read that file”)