DeveloperBreeze

Read and Display Filename from a Text File


# Open the file in read mode
with open('filename.txt', 'r') as fo:
    # Print the file name
    print(f'Filename is: {fo.name}')

    # Read and print the contents of the file
    content = fo.read()
    print('File Content:')
    print(content)

    # Count and display the number of lines in the file
    fo.seek(0)  # Reset pointer to the beginning
    lines = fo.readlines()
    print(f'Total Lines: {len(lines)}')

Continue Reading

Handpicked posts just for you — based on your current read.

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!