0% found this document useful (0 votes)
3 views1 page

Handling File - Py

The document contains Python functions for processing files, including counting lines in a text file, logging error lines to a separate file, adding journal entries with timestamps, and summing integers from a file while handling exceptions. Each function includes error handling for file operations and data processing. The code demonstrates basic file I/O and exception management in Python.

Uploaded by

chino0.9997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Handling File - Py

The document contains Python functions for processing files, including counting lines in a text file, logging error lines to a separate file, adding journal entries with timestamps, and summing integers from a file while handling exceptions. Each function includes error handling for file operations and data processing. The code demonstrates basic file I/O and exception management in Python.

Uploaded by

chino0.9997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

def process_file():

with open("data.txt", 'r') as file:


data = file.readlines()
line_count = len(data)
print(f"The Total Number Of lines Are: {line_count}")

error_lines = [line for line in data if 'error' in line.lower()]


for line in error_lines:
print(line.strip())

with open("errors.log", 'w') as log_file:


log_file.writelines(error_lines)

from datetime import datetime as time


def journal_entery():
entry = input("Enter Your Journal Entry: ")
timestamp = time.now().strftime('%Y-%m-%d %H:%M-%S')
entry_with_timestamp = f"{timestamp} -{entry}"
"\n"
try:
with open('journal.txt','a') as journalfile:
journalfile.write(entry_with_timestamp)
print('Entry added to data.txt')
except Exception as err:
print(f'{err} Occurred')

def numder_sum():
try:
with open('numbers.txt','r') as file:
numbers = file.readlines()

total = 0
for line in numbers:
try:
total += int(line.strip())
except ValueError:
print(f'skipping non-integer value: {line.strip()}')

print(f'sum of integers in numbers.txt: {total}')

except FileNotFoundError:
print('Error: numbers.txt file not found')

You might also like