Files. Volatile and Non-Volatile Storage So far we have stored data using variables These...

Post on 18-Jan-2016

227 views 0 download

Tags:

transcript

Files

Files.pptx

Volatile and Non-Volatile Storage So far we have stored data using variables

These variables are held in the computer’s volatile memory or RAM; when you close Python, your data is deleted

We can also hold data in non-volatile files on a hard disk or USB stick

These data files are not deleted when you close Python

2 of 30

Files.pptx

File Types There are two different file types:

Text Files Human-readable Strings are sent to disk files They can be opened with programs like Notepad They are useful for simple, unstructured data They are larger than binary files because they store whitespace

Binary Files Not directly human-readable Take up less space Not usually portable between programming languages Can be used to store more complex data

3 of 30

Files.pptx

Writing To a Text File First, you must open the file:

The file handle is a variable representing a stream to a disk file

The file name can have any extension but .txt is conventional for text files

The mode tells Python how you want to interact with the file. The wt mode means open a text file for writing

Not specifying a path indicates that the file is in the same directory as the program

file handle path file name mode

4 of 30

Files.pptx

Writing to a Text File Now you can write string(s) to the file:

You must include a newline character at the end of each line to separate them

Finally, you should close the file:

This flushes the stream and closes your connection to the file 5 of 30

Files.pptx

Try This… You might need to change the path to somewhere

you can write to or leave it off completely.

When you have run this program, find the file and open it with Notepad

6 of 30

Files.pptx

Reading from a Text File First you must open the file in read mode:

Then you can read the file, line by line:

The readline() function returns the next line from the file and it is stored in the ‘data’ variable

7 of 30

Why is the end = “” used here?

Files.pptx

Try This…

Add this to your code 8 of 30

Files.pptx

How many lines? Did you notice that we had two readline() statements?

We need one readline() for each line in the file. What if the file has many lines or we don’t know how many lines there are?

We can use an iterator, just like we did with sequences…

9 of 30

Files.pptx

Appending… If you open an existing file in write mode, it will be deleted!

You can open it in append mode if you want to avoid this…

The new lines are added at the end

If the files doesn’t already exist, it will be created 10 of 30

Files.pptx

Exercise Write a program which asks the user to enter a series

of sentences and writes them to a file. Allow an empty input to indicate that the user has finished.

Once you have checked this is working correctly, add code which reads back all the data from the file and displays it on the screen.

11 of 30

Files.pptx

Exercise Solution Part 1 Write the code which allows the user to enter

sentences and stop when they enter a blank

12 of 30

Files.pptx

Exercise Solution Part 2 Add in the file handling

Find the file and make sure it contains the sentences you entered

13 of 30

Files.pptx

Exercise Solution Part 3 Ensure the blank line isn’t written to the file

14 of 30

Files.pptx

Exercise Solution Part 4 Read the sentences and display them

15 of 30

Comma Separated Files Comma Separated (CSV) file are a standard way of

storing text data. E.g.name,email,phoneFred,fred@fred.com,0208-12901234Sue,sue@sue.com,01727-837172

Fields are separated by commas Records are separated by line breaks There is an optional field name list on the first line You could open this using Excel and get a spreadsheet

Files.pptx

CSV Files and Multi-Dimensional Lists How could a CSV file relate to a multi-dimensional list?

Have a look at the previous CSV example again:name,email,phoneFred,fred@fred.com,0208-12901234Sue,sue@sue.com,01727-837172

17 of 30

ListsList of lists… a multi-dimensional list

List of data labels

CSV Files and Multi-Dimensional Lists Each record is a list holding data for one friend, e.g.

The whole file is a multi-dimensional list e.g.

18 of 30

Files.pptx

How could we create the CSV File?

How could we write a CSV file for this multi-dimensional list?

Each line in the file could represent a list, e.g. a friend But we can’t directly write a list to a file, e.g.

19 of 30

Files.pptx

How could we do this? We could try converting the list to a string, e.g.

That seems to work but what did it actually write?['Fred', 'fred@fred.com', '0208-12901234'] When we read it back we would have to:

remove the quotes split the line at the commas remove the square brackets manually create a list from it.

This is possible but there is an easier way20 of 30

The csv Module - Writing The csv module does all of this for you First open a file for writing as usual:

Create a csv writer:

Iterate over your data list and write each list:

* use the option for any csv files to allow Python to handle the rows and newlines

*

Files.pptx

Exercise: Writing to a CSV File Download the file called highScores.py program from

the VLE

Add some code at the end of the program which writes the high scores list to a file called scores.txt

22 of 30

Files.pptx

Exercise Solution

23 of 30

Files.pptx

The csv Module - Reading First open a csvfile for reading as usual:

Create a csv reader:

Iterate over the csv_reader object and display:

Or append the row (list) to the friends list:

24 of 30

Files.pptx

Exercise: Reading from a CSV File Write a new program which reads the data from

scores.txt and outputs it in the following format:<name> scored <score> in <game>

25 of 30

Files.pptx

Exercise Solution

26 of 30

Files.pptx

File Offsets When you read a line from a file, a pointer moves to

the next line to be readFred,Game 1,3049John,Game 2,3943

If you want to move to the beginning of the file again then you must specifically move the file pointer

27 of 30

next line pointer

Files.pptx

File Offsets

28 of 30

Files.pptx

File Access in Pseudo Codecreate new_scores arrayopen csv text file for readingWHILE (NOT end of file)

read next line from fileappend line to new_scores array

END WHILE

29 of 30

Files.pptx

File Access using a Flowchart

30 of 30