DEV Community

Cover image for Python File methods
Python64
Python64

Posted on

Python File methods

In the Python programming language, you can do file system operations. This includes the ability to read or write files from code.

But the File class has many more functions.

File objects using open function, can use all the methods listed below. Besides opening, read a line and write a line, there's more you can do.

#!/usr/bin/env python

myFile = open("coffee.txt", 'r')
...
No. method and description
1 File.close() Close the file. After closing the file can not read and write operations.
2 File.flush() Internal refresh buffer files directly to the internal buffer of data written to the file immediately, rather than passively waiting for the output buffer written.
3 File.fileno() Returns an integer file descriptor (file descriptor FD integer), it can be used in a method as read some of the underlying operating os module.
4 File.isatty() If the file is connected to a terminal device returns True, otherwise False.
5 File.next() Returns the file the next line.
6 File.read() Reading the specified number of bytes from the file, it is not given, or if the reading of all negative.
7 File.readline() Read the entire line, including the "\n" character.
8 File.readlines() Read all rows and returns a list, if a given sizeint> 0, returns the sum of approximately sizeint byte line, the actual read value may be larger than the sizeint, because of the need to fill the buffer.
9 File.seek() Set the current position of the file
10 File.tell() Returns the current file position.
11 File.truncate() Intercepting files, taken byte size specified by default is the current file position.
12 File.write(str) Writes string to the file, returns the character written.
13 File.writelines(sequence) Write a list of strings to the file, if necessary wrap will have to add their own line breaks each line.

Top comments (1)

Collapse
 
mohsin708961 profile image
{{7*7}}

Awesome