"Python File Methods: File Manipulation Functions

thumb_up 1  ·  sell Python file methods, File manipulation in Python, File handling functions in Python

A file object is created using open() function. The file class defines the following methods with which different file IO operations can be done. The methods can be used with any file like object such as byte stream or network stream.

Sr.No. Methods & Description
1

file.close()

Close the file. A closed file cannot be read or written any more.

2

file.flush()

Flush the internal buffer, like stdio's fflush. This may be a no-op on some file-like objects.

3

file_fileno()

Returns the integer file descriptor that is used by the underlying implementation to request I/O operations from the operating system.

4

file.isatty()

Returns True if the file is connected to a tty(-like) device, else False.

5

next(file)

Returns the next line from the file each time it is being called.

6

file.read([size])

Reads at most size bytes from the file (less if the read hits EOF before obtaining size bytes).

7

file.readline([size])

Reads one entire line from the file. A trailing newline character is kept in the string.

8

file.readlines([sizehint])

Reads until EOF using readline() and return a list containing the lines. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read.

9

file.seek(offset[, whence])

Sets the file's current position

10

file.tell()

Returns the file's current position

11

file.truncate([size])

Truncates the file's size. If the optional size argument is present, the file is truncated to (at most) that size.

12

file.write(str)

Writes a string to the file. There is no return value.

13

file.writelines(sequence)

Writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings.

Let us go through the above methods briefly.

 

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back