Welcome to the File Handling Activity! This README will guide you through the various files and their purposes, as well as the steps to complete the class activity.
This script demonstrates how to write a dictionary to a JSON file.
import json
data = {"name": "Alice", "age": 25}
with open('data.json', 'w') as file:
json.dump(data, file, indent=4)This script shows how to read data from a JSON file and print it.
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(json.dumps(data, indent=4))This script illustrates the use of a finally block to ensure a file is closed after attempting to read it.
try:
file = open('example.txt', 'r')
data = file.read()
except FileNotFoundError:
print("File not found!")
finally:
file.close()This script demonstrates basic file operations: opening, reading, and closing a file.
file = open('example.txt', 'r')
data = file.readlines()
print(data)
file.close()This script shows basic error handling when attempting to open a non-existent file.
try:
file = open('non_existent_file.txt', 'r')
except FileNotFoundError:
print('File not found!')This script demonstrates how to write text to a file.
file = open('example_2.txt', 'w')
file.write("Hello, World!\nThis is a test.")
file.close()This text file contains a short story.
Once upon a time in a small village nestled between rolling hills, there lived a young girl named Lily...This text file contains a simple message.
Hello, World!
This is a test.This script provides solutions to the class activity, including reading from a text file and writing to a JSON file.
import json
def read_text_file(filename):
try:
with open(filename, 'r') as file:
content = file.read()
print("Text File Content:")
print(content)
except FileNotFoundError:
print(f"The file '{filename}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")
def write_json_file(data, filename):
try:
with open(filename, 'w') as file:
json.dump(data, file)
print(f"Data successfully written to {filename}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
read_text_file('example.txt')
sample_data = {"name": "Alice", "age": 25, "city": "New York"}
write_json_file(sample_data, 'data.json')This script outlines the assignment for the class activity.
'''
Assignment:
1. Read content from a text file named 'input.txt'.
2. Convert the content to a dictionary format.
3. Write the dictionary data to a JSON file named 'output.json'.
4. Implement exception handling to manage potential errors such as file not found or read/write errors.
'''- Read content from a text file: Use the
read_text_filefunction to read content frominput.txt. - Convert the content to a dictionary: Parse the content as needed to create a dictionary.
- Write the dictionary to a JSON file: Use the
write_json_filefunction to write the dictionary tooutput.json. - Implement exception handling: Ensure your code handles potential errors gracefully.
Good luck, and happy coding!