Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files Tutor: You will need ….

Similar presentations


Presentation on theme: "Files Tutor: You will need …."— Presentation transcript:

1 Files Tutor: You will need …

2 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 Files.pptx

3 File Types There are two different file types: Text Files Binary 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 Files.pptx

4 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 Files.pptx

5 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 Files.pptx

6 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 Files.pptx

7 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 Why is the end = “” used here? Files.pptx

8 Try This… Add this to your code Files.pptx

9 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… Files.pptx

10 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 Files.pptx

11 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. Files.pptx

12 Exercise Solution Part 1
Write the code which allows the user to enter sentences and stop when they enter a blank Files.pptx

13 Exercise Solution Part 2
Add in the file handling Find the file and make sure it contains the sentences you entered Files.pptx

14 Exercise Solution Part 3
Ensure the blank line isn’t written to the file Files.pptx

15 Exercise Solution Part 4
Read the sentences and display them Files.pptx

16 Comma Separated Files Comma Separated (CSV) file are a standard way of storing text data. E.g. name, ,phone 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

17 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, ,phone List of data labels List of lists… a multi-dimensional list Lists Files.pptx

18 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.

19 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. Files.pptx

20 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', ' '] 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 way Files.pptx

21 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 *

22 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 Files.pptx

23 Exercise Solution Files.pptx

24 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: Files.pptx

25 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> Files.pptx

26 Exercise Solution Files.pptx

27 File Offsets When you read a line from a file, a pointer moves to the next line to be read Fred,Game 1,3049 John,Game 2,3943 If you want to move to the beginning of the file again then you must specifically move the file pointer next line pointer Files.pptx

28 File Offsets Files.pptx

29 File Access in Pseudo Code
create new_scores array open csv text file for reading WHILE (NOT end of file) read next line from file append line to new_scores array END WHILE Files.pptx

30 File Access using a Flowchart
Files.pptx


Download ppt "Files Tutor: You will need …."

Similar presentations


Ads by Google