File Methods
flowchart TD
A["open(path, mode)"] --> B{"mode"}
B -->|"r"| C["read only; FileNotFoundError if missing"]
B -->|"w"| D["TRUNCATES to 0 bytes immediately, creates if missing"]
B -->|"a"| E["write only, always at the end; creates if missing"]
B -->|"x"| F["create only; FileExistsError if it exists"]
B -->|"r+"| G["read AND write, keeps the contents"]
B -->|"w+"| H["read and write, but TRUNCATES first"]
D --> I["the data is gone before you write a byte"]
H --> I
G --> J["this is the one people mean by 'open for editing'"]
File Methods
Section titled “File Methods”This tutorial will cover the different methods that can be used to read and write to files.
Table of Methods
Section titled “Table of Methods”| S.No. | Method | Description | Example |
|---|---|---|---|
| 1 | open() | Opens a file and returns a file object | file = open("file.txt", "r") |
| 2 | close() | Closes a file | file.close() |
| 3 | read() | Reads the entire file and returns a string | file.read() |
| 4 | readline() | Reads a single line from the file | file.readline() |
| 5 | readlines() | Reads all the lines from the file and returns a list | file.readlines() |
| 6 | write() | Writes a string to the file | file.write("Hello World") |
| 7 | writelines() | Writes a list of strings to the file | file.writelines(["Hello", "World"]) |
| 8 | seek() | Changes the current position of the file pointer | file.seek(0) |
| 9 | tell() | Returns the current position of the file pointer | file.tell() |
| 10 | truncate() | Truncates the file to the given size | file.truncate(10) |
| 11 | flush() | Flushes the internal buffer | file.flush() |
| 12 | fileno() | Returns the file descriptor | file.fileno() |
| 13 | isatty() | Returns True if the file is connected to a terminal | file.isatty() |
File Modes
Section titled “File Modes”The file modes are used to specify the purpose of opening a file. The following table lists the different file modes available in Python.
| S.No. | Mode | Description |
|---|---|---|
| 1 | r | Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. |
| 2 | rb | Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. |
| 3 | r+ | Opens a file for both reading and writing. The file pointer placed at the beginning of the file. |
| 4 | rb+ | Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file. |
| 5 | w | Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
| 6 | wb | Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
| 7 | w+ | Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
| 8 | wb+ | Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
| 9 | a | Opens a file for appending. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for writing. |
| 10 | ab | Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for writing. |
| 11 | a+ | Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for reading and writing. |
| 12 | ab+ | Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for reading and writing. |
| 13 | x | Creates a new file. If the file already exists, the operation fails. |
| 14 | x+ | Creates a new file. If the file already exists, the operation fails. |
| 15 | xb | Creates a new file in binary format. If the file already exists, the operation fails. |
| 16 | xb+ | Creates a new file in binary format. If the file already exists, the operation fails. |
| 17 | t | Opens in text mode. (default) |
| 18 | b | Opens in binary mode. |
| 19 | + | Opens a file for updating (reading and writing) |
| 20 | U | Universal newline mode. |
| 21 | rU | Opens a file for reading in universal newline mode. (deprecated) |
| 22 | wU | Opens a file for writing in universal newline mode. (deprecated) |
| 23 | rbU | Opens a file for reading in universal newline mode. (deprecated) |
| 24 | wbU | Opens a file for writing in universal newline mode. (deprecated) |
File Object Attributes
Section titled “File Object Attributes”The following table lists the different file object attributes available in Python.
| S.No. | Attribute | Description |
|---|---|---|
| 1 | closed | Returns True if the file is closed, False otherwise. |
| 2 | encoding | Returns the encoding of the file. |
| 3 | mode | Returns the mode of the file. |
| 4 | name | Returns the name of the file. |
| 5 | newlines | Returns a tuple of all the line terminators in the file. |
| 6 | softspace | Returns False if space explicitly required with print, True otherwise. |
open() Method
Section titled “open() Method”The open() method opens a file and returns a file object. The following example opens a file named “file.txt” in read mode and prints its content.
Hello Worldfile = open("file.txt", "r")
print(file.read())Output:
C:\Users\username>python open.py
Hello WorldIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The read() method reads the entire content of the file and returns it as a string.
close() Method
Section titled “close() Method”The close() method closes a file. A closed file cannot be read or written any more. Any operation, which requires that the file be opened will raise a ValueError after the file has been closed. Calling close() more than once is allowed.
Hello Worldfile = open("file.txt", "r")
print(file.read())
file.close()Output:
C:\Users\username>python close.py
Hello WorldIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The read() method reads the entire content of the file and returns it as a string. The close() method closes the file.
read() Method
Section titled “read() Method”The read() method reads the entire content of the file and returns it as a string. The following example opens a file named “file.txt” in read mode and prints its content.
Hello Worldfile = open("file.txt", "r")
print(file.read())Output:
C:\Users\username>python read.py
Hello WorldIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The read() method reads the entire content of the file and returns it as a string.
readline() Method
Section titled “readline() Method”The readline() method reads a single line from the file. The following example opens a file named “file.txt” in read mode and prints its first line.
Hello Worldfile = open("file.txt", "r")
print(file.readline())Output:
C:\Users\username>python readline.py
Hello WorldIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The readline() method reads the first line of the file and returns it as a string.
readlines() Method
Section titled “readlines() Method”The readlines() method reads all the lines from the file and returns a list. The following example opens a file named “file.txt” in read mode and prints all its lines.
Hello World
Hii Worldfile = open("file.txt", "r")
print(file.readlines())Output:
C:\Users\username>python readlines.py
['Hello World\n', 'Hii World']In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The readlines() method reads all the lines from the file and returns a list.
write() Method
Section titled “write() Method”The write() method writes a string to the file. The following example opens a file named “file.txt” in write mode and writes a string to it.
file = open("file.txt", "w")
file.write("Hello World")Output:
C:\Users\username>python write.pyHello WorldIn the above example, we open a file named “file.txt” in write mode. The file object is stored in the variable file. The write() method writes a string to the file.
writelines() Method
Section titled “writelines() Method”The writelines() method writes a list of strings to the file. The following example opens a file named “file.txt” in write mode and writes a list of strings to it.
file = open("file.txt", "w")
file.writelines(["Hello", "World"])Output:
C:\Users\username>python writelines.pyHelloWorldIn the above example, we open a file named “file.txt” in write mode. The file object is stored in the variable file. The writelines() method writes a list of strings to the file.
seek() Method
Section titled “seek() Method”The seek() method changes the current position of the file pointer. The following example opens a file named “file.txt” in read mode and changes the current position of the file pointer.
Hello Worldfile = open("file.txt", "r")
file.seek(6)
print(file.read())Output:
C:\Users\username>python seek.py
WorldIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The seek() method changes the current position of the file pointer to 6. The read() method reads the content of the file from the current position of the file pointer.
tell() Method
Section titled “tell() Method”The tell() method returns the current position of the file pointer. The following example opens a file named “file.txt” in read mode and prints the current position of the file pointer.
Hello Worldfile = open("file.txt", "r")
file.seek(6)
print(file.tell())Output:
C:\Users\username>python tell.py
6In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The seek() method changes the current position of the file pointer to 6. The tell() method returns the current position of the file pointer.
truncate() Method
Section titled “truncate() Method”The truncate() method truncates the file to the given size. The following example opens a file named “file.txt” in read mode and truncates it to 10 bytes.
Hello Worldfile = open("file.txt", "r+")
file.truncate(10)
print(file.read())Output:
C:\Users\username>python truncate.py
Hello WorIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The truncate() method truncates the file to 10 bytes. The read() method reads the content of the file.
flush() Method
Section titled “flush() Method”The flush() method flushes the internal buffer. The following example opens a file named “file.txt” in write mode and flushes the internal buffer.
file = open("file.txt", "w")
file.write("Hello World")
file.flush()Output:
C:\Users\username>python flush.pyHello WorldIn the above example, we open a file named “file.txt” in write mode. The file object is stored in the variable file. The write() method writes a string to the file. The flush() method flushes the internal buffer.
fileno() Method
Section titled “fileno() Method”The fileno() method returns the file descriptor. The following example opens a file named “file.txt” in read mode and prints its file descriptor.
Hello Worldfile = open("file.txt", "r")
print(file.fileno())Output:
C:\Users\username>python fileno.py
3In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The fileno() method returns the file descriptor.
isatty() Method
Section titled “isatty() Method”The isatty() method returns True if the file is connected to a terminal, False otherwise. The following example opens a file named “file.txt” in read mode and prints True if the file is connected to a terminal, False otherwise.
Hello Worldfile = open("file.txt", "r")
print(file.isatty())Output:
C:\Users\username>python isatty.py
FalseIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The isatty() method returns False because the file is not connected to a terminal.
closed Attribute
Section titled “closed Attribute”The closed attribute returns True if the file is closed, False otherwise. The following example opens a file named “file.txt” in read mode and prints True if the file is closed, False otherwise.
Hello Worldfile = open("file.txt", "r")
print(file.closed)
file.close()
print(file.closed)Output:
C:\Users\username>python closed.py
False
TrueIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The closed attribute returns False because the file is not closed. The close() method closes the file. The closed attribute returns True because the file is closed.
encoding Attribute
Section titled “encoding Attribute”The encoding attribute returns the encoding of the file. The following example opens a file named “file.txt” in read mode and prints its encoding.
Hello Worldfile = open("file.txt", "r")
print(file.encoding)Output:
C:\Users\username>python encoding.py
cp1252In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The encoding attribute returns the encoding of the file.
mode Attribute
Section titled “mode Attribute”The mode attribute returns the mode of the file. The following example opens a file named “file.txt” in read mode and prints its mode.
Hello Worldfile = open("file.txt", "r")
print(file.mode)Output:
C:\Users\username>python mode.py
rIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The mode attribute returns the mode of the file.
name Attribute
Section titled “name Attribute”The name attribute returns the name of the file. The following example opens a file named “file.txt” in read mode and prints its name.
Hello Worldfile = open("file.txt", "r")
print(file.name)Output:
C:\Users\username>python name.py
file.txtIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The name attribute returns the name of the file.
newlines Attribute
Section titled “newlines Attribute”The newlines attribute returns a tuple of all the line terminators in the file. The following example opens a file named “file.txt” in read mode and prints its line terminators.
Hello World
Hii Worldfile = open("file.txt", "r")
print(file.newlines)Output:
C:\Users\username>python newlines.py
('\r\n', '\r\n')In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The newlines attribute returns a tuple of all the line terminators in the file.
softspace Attribute
Section titled “softspace Attribute”The softspace attribute returns False if space explicitly required with print, True otherwise. The following example opens a file named “file.txt” in read mode and prints False if space explicitly required with print, True otherwise.
Hello Worldfile = open("file.txt", "r")
print(file.softspace)
print("Hello World")
print(file.softspace)Output:
C:\Users\username>python softspace.py
False
Hello World
TrueIn the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The softspace attribute returns False because space explicitly required with print. The print() function prints “Hello World”. The softspace attribute returns True because space is not explicitly required with print.
Conclusion
Section titled “Conclusion”In this tutorial, you learned about the different methods that can be used to read and write to files. In the next tutorial, you will learn about the different file attributes that can be used to get information about a file. For more information on the file methods, refer to the official documentation. For more tutorials, visit our Python Central Hub.
Check yourself
Section titled “Check yourself”-
You open an existing 8-byte file with mode `'w'` and then, before writing, your program raises. What is in the file?
`w` truncates on open, before any write. Measured: the file was 0 bytes immediately after `open()`. There is no undo.
pch.quizShowAnswer
B — Nothing — it was truncated when it was opened — `w` truncates on open, before any write. Measured: the file was 0 bytes immediately after `open()`. There is no undo.
-
Which mode lets you read AND write without destroying the existing contents?
`r+` opens for reading and writing and keeps the contents. `w+` is also read/write but truncates first — measured at 0 bytes on open.
pch.quizShowAnswer
B — `r+` — `r+` opens for reading and writing and keeps the contents. `w+` is also read/write but truncates first — measured at 0 bytes on open.
-
What does mode `'x'` do when the file already exists?
`x` is create-only. It refuses rather than clobbering, which makes it the safe choice when overwriting would be a bug.
pch.quizShowAnswer
C — Raises FileExistsError — `x` is create-only. It refuses rather than clobbering, which makes it the safe choice when overwriting would be a bug.
-
What is the safest way to replace a file's contents when the data matters?
Writing elsewhere and replacing means a crash mid-write leaves the original intact. `os.replace` is atomic on the same filesystem, and is consistent across platforms unlike `os.rename`.
pch.quizShowAnswer
B — Write to a temporary file, then `os.replace` it into position — Writing elsewhere and replacing means a crash mid-write leaves the original intact. `os.replace` is atomic on the same filesystem, and is consistent across platforms unlike `os.rename`.
Try it: File Methods Exercises
Section titled “Try it: File Methods Exercises”Exercise 1 – readline() vs readlines()
Section titled “Exercise 1 – readline() vs readlines()”Exercise 2 – tell() and seek()
Section titled “Exercise 2 – tell() and seek()”Exercise 3 – writelines()
Section titled “Exercise 3 – writelines()”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading