Working with files

File (file descriptor) in Python is object (as everything else)

  • Opening file: f = open(<path>[, mode="<modificator>"][, encoding="utf-8"])

  • Modificators:

    • r โ€“ read only (default method)

    • r+ โ€“ read and write

    • w โ€“ write (with flushing) (> in shell)

    • a โ€“ appending (>> in shell)

    • b โ€“ binary mode

Basic operations

  • Reading:

    • f.read(N) โ€“ read N bytes (into string)

    • f.readline() โ€“ read one line (with separator)

    • f.readlines() โ€“ read all file in a list

  • f.close() โ€“ closing file (Python closes it on exit)

  • Writing:

    • f.write(text) โ€“ write string to file (w/o closing)

    • f.writelines(lines) โ€“ write list of strings to file

    • f.flush() โ€“ sync buffer to disk (w/o closing file)

Changing position:

From Python 3.2: In text files (those opened without a "b" in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)).

  • f.tell() โ€“ returns the fileโ€™s current position

  • f.seek(N, [whence=0]) โ€“ shift N bytes from the beginning where whence (direction) can be:

    • 0 (from beginning)

    • 1 (from current position)

    • 2 (from the end)

Example:

Here we are using so-called "context managers" - via *with as" keywords. This allows us to not worry about closing the file descriptor after block of with-as code.

๐Ÿช„ Code:

filename = "/tmp/1.txt"
# filename = r"D:\tmp.txt"

with open(filename, "w") as f:
    f.write("Test string!!!\n")
    
with open(filename) as f:
    print(f.read())
    
print(f.closed)

๐Ÿ“Ÿ Output:

Test string!!!

True

๐Ÿช„ Code:

%ls -la /tmp/1.txt
-rw-r--r-- 1 jovyan users 15 Jun 20 11:52 /tmp/1.txt

Last updated