Skip to content

Operations on files

Python allows several possibilities to manage files, read or save data. It provides functionality to use csv, xml, yaml, zip and tar archive and many others.

  • For example we can save some game progress to read it next time.
  • We can store some analysis data in external file and use it to generate counts, or chart.
  • We can transform data to other format i.e. Excel.

Best way to start is to use simple text format methods.

Function open()

To use operations on files you need to add reference. Use included function open(). open(). It requires one parameter - file location that we want to open. Path can be relative, or absolute. By default file is open with read-only access and in simple text format. To open binary file or save, modify file you need to add open mode as second parameter. not-writable

On above screenshot you can see that Python will not allow you to save file if it's not defined in open mode. Files operations summary:

  • Function open() return a file object.
  • Object contains methods and parameters that offer collect file details and changes them.
  • File location is required parameter (string type).
  • Additionally you can add open mode (read, write etc.).
  • File and a file object are two different things!
  • To close file use the function .close().

File open modes

File can be opened in several modes, depend on what it will be used for. Mode is the second parameter of the open() function.

  • "r" - read access (default).
  • "w" - write, if a file doesn't exist it will be created, also if an older file exists it will be overwritten.
  • "x" - execute, it will fail if a file already exists.
  • "a" - add, if a file doesn't exist it will be created, if exists new data will be added.
  • "b" - binary mode.
  • "t" - text mode (default).
  • "+" - this means that cursor will set at the end of a file. By default it's at the top of file.

You can use several attributes i.e. a+b means binary file open with write access, but if a file exists, it will not cut off and new add will get at the end of file. Tip If you need to open file in read/write mode you can use r+ Last parameter of an open function is encoding. Default value depend on operating system, in Windows it's cp1252, Linux - utf-8.

Closing file

At the end of your work you need to close it. Use close() method on object (the same as you used for open() function). Warning this below code is not safe:

    f = open('file.txt')

    # ...

    f.close()

There's no guarantee that between open and close there's no error. If there is an error, after open file, close function will not execute and file will left opened. Better idea is to use try block and close it in finally block. This will force to get a file closed (how to use block try-except-finally will be presented in the next step of the course).

    try:
        f = open('file.txt')
        # ...
    finally:
        f.close()

File open

Example 1

The easiest, however the most primitive, method to open a file:

  • It's not safe way.
  • If between open and close there's an error it will not close a file!
    f = open('file.txt')

    # ...

    f.close()

Example 2

    with open('file.txt') as file_variable:
        # ...

    print("OK")  # Block with will close file automatically
  • More "Python's" way to open a file.
  • You can use with to open a file.
  • Command with open a file context, which is inside.
  • Once context leaved file will get closed automatically.
  • It eliminates risk of closing an opened file.

In context block we get file access, using file object name that was set i.e. file_variable. You can use whatever name consistent with Python convention.

Read file

When file is opened we can read it line by line.

Example 1

Historical way: use method readline():

    f = open('file.txt')

    while(True):
        line = f.readline()
        if not line:
            break
        print(line)

Method readline() read one line every time it is used and automatically goes to next line, waiting for another execution. In case whole file is read, readline() return empty string (empty lines in file contain end of line char \n).

Example 2

Better way: use method readlines().

    with open('file.txt') as f:
        lines = f.readlines()

        for line in lines:
            print(line)

readlines() return list that each element is one line, which was readed from file. It can be useful when we don't want to read line by line but move on file in our way. Remember that readlines() will read whole file, no matter how large is it - watch out on RAM.

Example 3

Standard way: use an iteration returned by the object.

    with open('file.txt') as f:

        for line in f:
            print(line) 

You can iterate on a file object (we use f). In every loop iteration there's one (and next) line returned.

Example 4

Curiosity: there's a way to read byte by byte.

    with open('file.txt') as f:
        content = f.read(4)  # read first 4 chars from file
        f.tell()  # write 4, this is our file postition - readed 4 chars
        content = f.read(4)  # read next 4 chars from file
        f.tell()  # write 8
        f.seek(0)  # cursor gets back to file begining
  • To read a file content use method read().
  • If you use it without parameter it will open whole file.
  • If the file is large it can cause risk of RAM usage - you can use a number parameter that determines how many maximum bytes should be loaded.
  • Maximum means that the file can be smaller.
  • Next read function usage cause cursor moves to next, so we read from where it ended.
  • When we get to the end of the file, read method will return empty string.
  • To move cursor use the seek method.
  • tell method proves current cursor possition.

Save file

To save something in a file you need to open it with write access mode.

    with open('data/sample.txt', 'w') as f:  # open file with write access
        for i in range(10):
            f.write(f"This is the line number: {i}\n")  # Save to file, every time in new line

Save file summary:

  • To save data in a file you need to open it using correct mode (write, or add).
  • If the file is open successfully (we have correct access, there's a space on drive), we can use write() method to write data.
  • Method write() use char string (what we want to save in a file) as a parameter.
  • In above example we can see an opened file sample.txt from data catalogue (relative path).
  • File was opened in write mode, so if it didn't exist it was created; if existed, old data were delated (if you want to add something to old data, use add mode 'a').
  • There're 10 lines save in a file.