16
loading...
This website collects cookies to deliver better user experience
open()
. It is used to open a file in one of the following modes-fileObject = open(filename, mode)
f = open("file.txt")
is same as f = open("file.txt", 'r')
with open(filename, mode) as fileObject:
with open("file.txt", 'r') as myFile:
for text in myFile:
print(text)
<file.closed>
- returns True
if the file is closed and False
otherwise<file.name>
- returns the name of the opened file<file.mode>
- returns the mode in which the file was openedwith open("file.txt", 'r') as myFile:
# more code goes here...
fileObject.read(n) # 'n' is the no of bytes of data
# reading 8 characters from the file
with open("file.txt", 'r') as myFile:
myFile.read(8)
'Hello wo'
# reading all the content from the file
with open("file.txt", 'r') as myFile:
myFile.read()
'Hello world! This is content of the file'
Each line ends with a newline character '\n', which is counted as a single character
fileObject.readline(n) # 'n' is the no of bytes of data
# reading 10 characters from the first line
with open("file.txt", 'r') as myFile:
myFile.readline(10)
'Hello worl'
# reading the entire first line
with open("file.txt", 'r') as myFile:
myFile.readline()
'Hello world! This is the first line of the file'
fileObject.readlines()
with open("file.txt", 'r') as myFile:
data = myFile.readlines()
print(data)
['Hello world!\n', 'Hello world!\n', 'Hello world!\n', 'Hello world!\n']
splitlines()
function.with open("file.txt", 'r') as myFile:
lines = myFile.readlines()
for line in lines:
line_split = line.splitlines()
print(line_split)
['Hello World!']
['Hello World!']
['Hello World!']
['Hello World!']
open()
method and pass the name and mode for the file as arguments. fileObject = open(filename, mode)
Numerical values need to be converted into strings before passing as the argument
fileObject.write("This is some data")
>>> myFile = open("file.txt", 'w')
>>> myFile.write("Hello World!")
12
fileObject.writelines(object)
>>> myFile = open("file.txt", 'w')
>>> lines = ["line1\n", "line2\n", "line3\n"]
>>> myFile.writelines(lines)
seek()
and tell()
tell()
function returns the current position of the cursor or file handle of the file as an integer. This function takes no argument. When a file is opened in any mode other than 'append' mode, the initial value of tell()
function is zero.fileObject.tell()
seek()
function allows us to position the file handle at a specific point in the file.fileObject.seek(offset, ref)
# Creating and writing data to a file
myFile = open("file.txt", 'w')
myFile.write("Hello world!, this data is being written onto the file.")
myFile.close()
# reading the file and displaying the offset position before and after reading
myFile = open("file.txt", 'r')
print("default position of the cursor:", myFile.tell())
data = myFile.read()
offset = myFile.tell()
print("current position of the cursor:", offset)
# positioning the offset at the 10th position
offset = myFile.seek(10)
print("new position of the cursor", offset)
fileObject.close()
import os
os.remove(filename)
We pass the name of the file as an argument. If the file does not exist, this function returns an error.os.path.exist(filename)
# Deleting a file
import os
if os.path.exists("file.txt"):
os.remove("file.txt")
else:
print("This file does not exist!")