Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to read the first line in a file python

f = open("test.txt", 'r')
variable = f.readline(1)
print(variable)
Comment

read only the first line python

""" Read only the first line of a file """

with open('file.txt') as f:
    first_line = f.readline()
    print(first_line)
    
""" or """

f = open ('file.txt', 'r')
first_line = f.readline()
print (first_line)
Comment

get first line of file python

with open('myfile.txt') as f:
    first_line = f.readline()
Comment

how to read first lines of a file

with open("datafile") as myfile:
    head = [next(myfile) for x in range(N)]
print(head)
Comment

PREVIOUS NEXT
Code Example
Python :: setwd python 
Python :: pretty print pandas dataframe 
Python :: array of random integers python 
Python :: pandas change last row 
Python :: python flask query params 
Python :: show rows with a null value pandas 
Python :: python delete all files in directory 
Python :: discord.py clear command 
Python :: cv2.imshow 
Python :: flask boiler plate 
Python :: import sklearn linear regression 
Python :: user agent for python 
Python :: print numpy version 
Python :: create new django app 
Python :: read file line by line into list 
Python :: how to disable help command discord.py 
Python :: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123) 
Python :: filter by row contains pandas 
Python :: how to install flask module in vscode 
Python :: mean squared error python 
Python :: use python3 as default ubuntu 
Python :: pandas fill na with value from another column 
Python :: remove non-alphabetic pandas python 
Python :: python color text on windows 
Python :: importying listviewin django 
Python :: df from numpy array 
Python :: python list of dates between 
Python :: python create nested directory 
Python :: python install package from code 
Python :: conver all dict keys to str python 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =