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

PREVIOUS NEXT
Code Example
Python :: pipenv 
Python :: say command python 
Python :: pyperclip 
Python :: python control browse mouse selenium 
Python :: pandas conditional replace values in a series 
Python :: python split on first occurrence 
Python :: django timezone india 
Python :: exit all threads from within a thread python 
Python :: convert number to binary in python 
Python :: the system cannot find the file specified sublime text 3 python 
Python :: python read from txt file 
Python :: how to make a window in tkinter 
Python :: how to sort a list in python using lambda 
Python :: RuntimeWarning: invalid value encountered in true_divide 
Python :: nan float python 
Python :: round python 
Python :: what is a cube minus b cube 
Python :: how to invert a list in python 
Python :: discord get username slash command 
Python :: sin and cos in python 
Python :: How to get current CPU and RAM usage in Python? 
Python :: how to convert string to byte without encoding python 
Python :: django form widget 
Python :: get biggest value in array python3 
Python :: python trace table generator 
Python :: discord py get user by id 
Python :: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable 
Python :: map function using lambda in python 
Python :: python hello world 
Python :: how to iterate pyspark dataframe 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =