My issue
Error: [Errno 2] No such file or directory: ‘/mnt/codechef/numbers.txt’
Sum: 15
My code
def user_function():
file_path = "/mnt/codechef/numbers.txt"
sum_val = 0
try:
with open(file_path, 'r') as reader:
for line in reader:
num = int(line.strip()) # Convert line to integer
sum_val += num # Accumulate the sum
print("Sum:", sum_val) # Print the computed sum
except IOError as e:
print("Error:", e) # Print the error message if file operation fails
# Call the function to execute
user_function()
Learning course: Python Programming
Problem Link: Coding problem - 2 in Python Programming