- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read only the first line of a file with Python?
To read only the first line of a file, open the file in read mode and call readline method on the file object. For example,
f = open('my_file.txt', 'r') line = f.readline() print line f.close()
The above code reads first line from my_file.txt and prints to stdout. A safer approach would be using the with open syntax to avoid file from not closeing in case of an exception:
with open('my_file.txt', 'r') as f: print f.readline()
- How to read only 5 last line of the text file in PHP?
- How to read a file from command line using Python?
- How to read complete text file line by line using Python?
- How to read an entire line from a text file using Python?
- Read file line by line using C++
- How to read a text file in Selenium with python?
- Change a file attribute to read only in Java
- How to read a text file with C++?
- What are some of the fastest way to read a text file line by line using C#?
- How to read a text file in Python?
- Read last line from file in PHP
- How to read text file into a list or array with Python?
- How to open a file in read and write mode with Python?
- How to read CSV file in Python?
- How to read JSON file in Python