Python File Methods

Updated: May 18



A file object permits us to utilize, access and control all the user accessible files. One can read and write any such files. At the point when a document activity comes up short for an I/O-related explanation, the special case IOError is raised. This incorporates circumstances where the activity isn't characterized for reasons unknown, like seek() on a tty gadget or composing a file opened for reading.

There are following method in file which are:

open(): Opens a file in given access mode.

open(file_address, access_mode)

Examples of accessing a file: A file opens by a built-in function is known as open(). This function takes in the file’s address and the access_mode and returns a file object.

There are various types of access_modes:

r: Opens a file for reading only

r+: Opens a file for both reading and writing

w: Opens a file for writing only

w+: Open a file for writing and reading.

a: Opens a file for appending

a+: Opens a file for both appending and reading

read([size]): It reads the complete file and returns it contents in a string form. It reads at most size bytes from the file. If the size argument is negative or omitted, read all data until EOF is reached.

# Reading a file

f = open(__file__, 'r')

#read()

text = f.read(10)

print(text)

f.close()



readline([size]): It reads the first line of the file. If the size argument is current and non-negative, it is a maximum byte count and an unfinished line may be returned.

# Reading a line in a file

f = open(__file__, 'r')

#readline()

text = f.readline(20)

print(text)

f.close()

readlines([sizehint]): It reads the entire file line by line and updates each line to a list which is returned. It reads until EOF using readline() and return a list containing the read lines. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totaling around sizehint bytes are read.

# Reading a file

f = open(__file__, 'r')

#readline()

text = f.readlines(25)

print(text)

f.close()

write(string): It writes the specified string to the file. It has no return value.

# Writing a file

f = open(__file__, 'w')

line = 'Welcome Career Bodh\n'

#write()

f.write(line)

f.close()

Different mode:

# Reading and Writing a file

f = open(__file__, 'r+')

lines = f.read()

f.write(lines)

f.close()

# Writing and Reading a file

f = open(__file__, 'w+')

lines = f.read()

f.write(lines)

f.close()

# Appending a file

f = open(__file__, 'a')

lines = 'Welcome Career Bodh\n'

f.write(lines)

f.close()

# Appending and reading a file

f = open(__file__, 'a+')

lines = f.read()

f.write(lines)

f.close()

writelines(sequence): It writes a list of strings to the file and has no return value.

# Writing a file

f = open(__file__, 'a+')

lines = f.readlines()

#writelines()

f.writelines(lines)

f.close()

tell(): It returns the current position of a file in the form of bytes.

# Telling the file object position

f = open(__file__, 'r')

lines = f.read(10)

#tell()

print(f.tell())

f.close()

seek(offset, from_where): It is used to change the position of file. Offset represent the number of bytes to be moved and from_where represent from where the bytes are to be moved.

# Setting the file object position

f = open(__file__, 'r')

lines = f.read(10)

print(lines)

#seek()

print(f.seek(2,2))

lines = f.read(10)

print(lines)

f.close()



flush(): Flushes the internal buffer, like stdio‘s fflush(). There are no return value in flush method and close() itself flushes the data but if we want to flush the data before closing the file then we use this method.

# Clearing the internal buffer before closing the file

f = open(__file__, 'r')

lines = f.read(10)

#flush()

f.flush()

print(f.read())

f.close()

fileno() : It returns a number that represents the stream, from the operating system's point of view .

# Getting the integer file descriptor

f = open(__file__, 'r')

#fileno()

print(f.fileno())

f.close()

isatty(): It returns whether the file stream is interactive or not.

# Checks if file is connected to a tty(-like) device

f = open(__file__, 'r')

#isatty()

print(f.isatty())

f.close()

truncate([size]): It resizes the file to a specified size.

# Truncates the file

f = open(__file__, 'w')

#truncate()

f.truncate(10)

f.close()

close(): It is used to close an open file. # Opening and closing a file

f = open(__file__, 'r')

#close()

f.close()



Recent Posts

See All

Python Modules

A module is a file containing Python definitions and statements. A module can characterize functions, classes and variables. A module can likewise incorporate runnable code. Gathering related code int

Command Line Arguments in Python

Command Line Arguments are the arguments that are specified after the name of the program in the command line shell of the operating system. Python gives different ways of dealing with these types of