0% found this document useful (0 votes)
74 views15 pages

Y10 05 CT26 Slides

reading files

Uploaded by

molaposk10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views15 pages

Y10 05 CT26 Slides

reading files

Uploaded by

molaposk10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Y10-05-CT26: Reading files

Y10-05-CT26: Reading files

Learning objectives
In this lesson you will learn how to:

• Open a file for reading


• Read lines from a text file
• Close a file.

For more detail on this topic, and additional learner activities, refer to
Topic 1.11 of the student book.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Working with data

Previously you have looked at how to work with data in Python.

You used lists and arrays to deal with data in one dimension.

You learned about two-dimensional structures that contain records.

So far, you have been hard coding this data, or asking the user to
enter it directly into the program.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

The problem…

Hard-coded data exists only within the code, and only when the
program is running.

In other words, the data is only stored in memory while the program
is running. It is not persistent.

In order to make data persistent, you need to use an external file to


store the data. It will then exist in storage, even after the program
finishes.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Structured data

When data is stored in records, it is considered to be organised.


Organised data is often called a database.

A common way of representing a database is to use a comma


separated values (CSV) file.

A text file is used to store this data persistently.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Structured data

These represent
column headings
and are optional.

This is a
single
record.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Reading a text file into Python

There are different ways of reading the data in from a file. The
method used will depend on the type of file and the structure of the
data inside it.

For the examples you will be working with, when you read data in
from a file, you will use lists – first one dimensional and then two
dimensional.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Open, read and close…

1. Declare a variable to store a reference to the file.


2. Use the open() function to access the file.
3. The input parameters are the filename, including the file
extension, and the opening mode (‘r’ is for read only mode).
4. Once finished with the file, close it with the close() procedure.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Reading in the file so it is useful

1. Declare a variable to store the data from the file as a list.


2. Use the readlines() function to read the separate lines of the file
into a list.
3. Close the file when finished reading.
4. Print out the result to see the data.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

The result

['ID,Username,MonthJoined,BirthMonth\n’,
'1,Alice,2,4\n','2,Bob,3,2\n’, '3,Charles,9,4\n’,
'4,Dinah,10,12\n’, '5,Edwardo,1,11\n’,
'6,Fran,8,3\n’, '7,Georgia,9,9\n']

In your console session, you’ll see this all on a single line.

It’s a bit of a mess at the moment!

Let’s look closer and see why this is the result…

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

What does ‘lines’ mean?

The term ‘lines’ has been mentioned several times.

When you look at a file in an editor, you can see the separate lines
clearly, but how are they represented in a computer?

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Representing a line

Each line is read as one single string with an escape character at


the end.

The \n is the ‘new line’ escape character. It is hidden from us, but it is
what the computer uses to recognise the end of a line.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Non-printable characters

Carriage CR Pushes the location for the next output back ‘\r’ 0x0D
Return to the far left.
Line Feed / LF Rolls the location for the next output down ‘\n’ 0x0A
New Line to the next line.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

String manipulation

In previous lessons, you learned how to work with and manipulate


strings.

You will need to use some of these functions to help you work with
the text in files.

In this instance, you need to remove the new line escape character
from each line as it is being inserted into the list.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-05-CT26: Reading files

Wrap up: you have learned how to…


 Open files for reading.
• Use the open() function with the filename and ‘r’ to open in
read mode.
 Read lines from text files.
• Use the readlines() function to read the contents of the file into
an array.
• OR loop through the open file and do something to each line.
 Close a file.
• Use the close() procedure.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.

You might also like