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")
Auf dieser Seite können Sie das Online-Video Read files using Python! 🔍 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Bro Code 13 Juli 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 30,197 Mal angesehen und es wurde von 735 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!