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)}')

Related Posts

More content you might like

Tutorial
php

Securing Laravel Applications Against Common Vulnerabilities

   composer require --dev enlightn/security-checker
   php artisan security:check
  • Use built-in Laravel features like query bindings, CSRF protection, and encryption to secure your application.
  • Validate and sanitize user inputs to prevent SQL injection and XSS.
  • Monitor and audit your application regularly for vulnerabilities.

Nov 16, 2024
Read More
Code
python

File Reading and Writing

No preview available for this content.

Jan 26, 2024
Read More
Code
json python

Append Data to JSON File

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!