Code Programming Tutorials, Guides & Best Practices
Explore 184+ expertly crafted code tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Code
python
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)}')
Jan 26, 2024
Read More