miércoles, 21 de agosto de 2024

reading file line by line

 # Define the path to your file

file_path = '/root/log.txt'


# Open the file and read it line by line

try:

    with open(file_path, 'r') as file:

        # Iterate over each line in the file with line numbers

        for line_number, line in enumerate(file, start=1):

            # Print the line number and the line content

            print(f"Line {line_number}: {line.strip()}")

except FileNotFoundError:

    print(f"The file at {file_path} was not found.")

except IOError as e:

    print(f"An I/O error occurred: {e}")


No hay comentarios:

Publicar un comentario