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")
In questa pagina del sito puoi guardare il video online Read files using Python! 🔍 della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Bro Code 13 luglio 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 30,197 volte e gli è piaciuto 735 spettatori. Buona visione!