How can I read only the first line of an input file in Python?
My file has the following content and I want to just get the header.
You can use readline() or readlines() to get the first row of an input file. Here is an example using your input data.
with open('testfile.txt', 'r') as f: rec = f.readline()print(rec)
with open('testfile.txt', 'r') as f: rec = f.readlines()print(rec.strip())
These codes will print the following: