File handling is one of the important feature in programming languages. Python provides a way to handle(read, write & modify) both text files as well as binary files. This tutorial will cover read(), readlines(), seek() and tell() functions in detail.
➠ There are various functions available in Python for File handling. Click the function to get more detail about that function.
fileVar = open("/complete_path_to_file/pythonTest.txt")
print(fileVar.read())
fileVar.close()
Output:
Line 1: This is Python File Handling
Line 2: This is Python File Handling Testing File
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
print(fileVar.read(22))
fileVar.close()
Output:
Line 1: This is Python
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
print(fileVar.readline())
fileVar.close()
Output:
Line 1: This is Python File Handling <newline Character>
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
fileVar.seek(16) #moving the cursor to 16th character
print(fileVar.readline())
fileVar.close()
Output:
Python File Handling <newline Character>
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
print(fileVar.readlines())
fileVar.close()
Output:
['Line 1: This is Python File Handling\n', 'Line 2: This is Python File Handling Testing File']
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
fileVar.seek(16) #moving the cursor to 16th character
print(fileVar.readlines())
fileVar.close()
Output:
['Python File Handling\n', 'Line 2: This is Python File Handling Testing File']
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
listVar = fileVar.readlines()
print(listVar[1])
fileVar.close()
Output:
Line 2: This is Python File Handling Testing File
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "r")
for x in fileVar:
print(x)
Output:
This is Python File Writing
This is Python's second File Writing line
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
print(fileVar.read())
fileVar.seek(0) #moving the cursor to beginning of file
print(fileVar.read()) #Reading the file again
fileVar.close()
Output:
Line 1: This is Python File Handling
Line 2: This is Python File Handling Testing File
0
Line 1: This is Python File Handling
Line 2: This is Python File Handling Testing File
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
fileVar.seek(16) #moving the cursor to 16th character
print(fileVar.readline())
fileVar.close()
Output:
Python File Handling <newline Character>
fileVar = open("/complete_path_to_file/pythonTest.txt", "rb")
print(fileVar.read(27).decode('utf-8'))
fileVar.seek(-11,1)
print(fileVar.read(20).decode('utf-8'))
fileVar.close()
Output:
Line 1: This is Python File
16
Python File Handling
fileVar = open("/complete_path_to_file/pythonTest.txt", "rb")
print(fileVar.read(27).decode('utf-8'))
fileVar.seek(-12,2)
print(fileVar.read(20).decode('utf-8'))
fileVar.close()
Output:
Line 1: This is Python File Handling <newline Character>
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
str_line1 = fileVar.readline()
print(fileVar.tell())
Output: 37