Loading ...

How to read and write files in Python

Manipulating files is called “I/O” in programming terms and Python offers the same facilities to read and write to files as most programming languages. The process consists of opening a file, reading or writing to it and then closing the file. Python uses the “open," “read," “write” and “close” statements as the main file I/O functions and all but the simplest of programs will need to use these statements at some point.

Loading ...

Open the file with the following statement: FileObject = open(“filename”, “mode”)

“FileObject” is a variable that will contain system data to handle the file and you can name this as you wish. Replace “filename” with the full path and file name you want to open. Replace “mode” with a file mode to tell Python how you want to open the file. If you just want to read the file, use “r” as the file mode. If you just want to write to the file, use “w” as the file mode. If you want to both read and write to the file, use “w+” as the file mode. Once you have opened the file in this way, you use the “FileObject” variable to read and write to it.

Read from a file with the “Var =” statement. “FileObject” is the variable you used with “open” to open the file. “Var” is variable name of your choice into which you will read file contents. Replace “count” with the number of bytes you want to read from the file. If you do not enter a “count," Python will read the whole file.

Read a line from a file -- as opposed to a set number of bytes -- with the “Var = FileObject.readline()” statement. This will read from the file until it finds the newline character or reaches the end of the file.

Write to a file with the “FileObject.write(data)” statement. “FileObject” is the variable you used with “open” to open the file. Replace “data” with the data you want to write to the file. For example, to write a line of text to a file use: FileObject.write(“This is a line of text\n”)

The “\n” at the end starts a new line after the line of text.

When you read from or write to a file, it does so at the current “file position." When you open a file with the “r” or “w” file modes, the file position is set to the start. If you then write to a file opened in "w" mode you would overwrite anything currently in the file. If you want to open a file and write to the end of it, open it with the “a” (for append) file mode. Alternatively, you can move around in a file by using the “seek” statement; for example, the “” would move the file position 100 bytes forward from the current position.

Close the file with the “FileObject.close()” statement. Closing the file frees up any system resources it was using and you can no longer read and write to the file until you open it again.


Wrap file I/O statements in exceptions to handle errors in file processing gracefully.

Loading ...

About the Author

Gordon Ansell has been an IT specialist since 1981, working for insurance companies, software houses, scientific institutions and television companies. Ansell’s roles have been wide and varied, including systems programmer, software engineer, technical author, systems administrator and project manager. Ansell has written more than 600 posts for one of the most popular Newcastle United blogs.

Loading ...