Basic

Opening a file

A file is bytes on a disk. To read one you ask for a path, and the operating system hands back those bytes. That one call hides the whole journey.

What you will see

gone

About 2 min of reading

Try It

Read a file and handle the miss.

1

Length of a short string.

data = "hello world" print(len(data))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
11
2

First four characters.

print("notes.txt"[:4])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
note
3

Slice of a read.

data = "abc123" print(data[:3])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
abc
4

Missing file caught.

try: open("nope.txt") except FileNotFoundError: print("gone")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
gone

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A path names a place, open asks for it, and the read call returns the bytes as text. A missing path raises instead.
1 name a path
↓
2 open the file
↓
3 read the bytes
↓
4 handle a miss
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

data = "hello world" sets nothing on screen
print(len(data)) 11
Why this works
A path is not a file

A path is text that names a place. Whether a file lives there is only known when you try, and trying can fail.

Bytes become text

The read call turns bytes into a string using an encoding. If the encoding is wrong, the text is wrong.

Examples

Read the whole file

with open("notes.txt") as f:
    data = f.read()
print(len(data))

The name is a path. The result is one string holding every byte.

The path is a string

name = "notes.txt"
with open(name) as f:
    print(f.read()[:4])

A path is just text. Build it from parts and the same call works.

A relative path

with open("data/in.txt") as f:
    print(len(f.read()))

Relative means from where the program runs. The same code fails if that changes.

An absolute path

with open("/tmp/x.txt") as f:
    print(f.read())

A leading slash names the place exactly. No dependence on the current folder.

Read twice needs two opens

with open("notes.txt") as f:
    first = f.read()
    second = f.read()
print(len(second))

The cursor sits at the end after the first read. The second read gets nothing.

Close releases the handle

f = open("notes.txt")
data = f.read()
f.close()
print(len(data))

Without with you close by hand. Forgetting it leaves a handle open.

A missing file raises

try:
    open("nope.txt")
except FileNotFoundError:
    print("gone")

A missing path is an error, not an empty string. Handle it or the program stops.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Reading line by line
Reading line by line →