Download - Programming for GCSE Topic 10.1: Files
![Page 1: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/1.jpg)
Programming for GCSE
Topic 10.1: Files
Teaching London Computing
William MarshSchool of Electronic Engineering and Computer Science
Queen Mary University of London
![Page 2: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/2.jpg)
Aims
• Be able to develop simple programs for text I/O to files
• Understanding teaching issues for files• Output is easier than input• Need to 'design' a file format
![Page 3: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/3.jpg)
Outline• File I/O Operation• Example programs• Issues of I/O: asymmetry of input and
output• Output: you know• Input: you don't know
• Input problem• Designing a file format
![Page 4: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/4.jpg)
BASIC FILE OPERATIONS
![Page 5: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/5.jpg)
Operations on Files
• open• File name: a string• Mode: 'r' (read) or 'w' (write) • Creates a 'file object'
• write• Write strings: lines end with \n
• readline• Read a line
• close
![Page 6: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/6.jpg)
What is a file?
• Sequence of bytes• What do these bytes represent?
• A text file has strings• Often organised in lines• Example line: "This is a line\n"
• What does the string represent?• Could be a number
![Page 7: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/7.jpg)
Where is the File – Buffering • File is on disk• BUT• Not every character causes a write to disk• Why not?• File is 'buffered'
• Usually, this is invisible• Can lead to strange errors
![Page 8: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/8.jpg)
EXAMPLE PROGRAMS
![Page 9: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/9.jpg)
Open – Write – Close import io
f = open("hello.txt", 'w')f.write("This is a line\n")f.write("This is a string ")f.write("followed by more on the line\n")f.close()
This is a lineThis is a string followed by more on the line
![Page 10: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/10.jpg)
Writing Numbers
• First convert the number to a string
import io
f = open("numbers.txt", 'w')f.write(str(10) + "\n")f.write(str(100) + "\n")f.close()
![Page 11: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/11.jpg)
Reading – readline
• Line from file becomes string• Includes the "\n"
import io
f = open("numbers.txt", 'r')line1 = f.readline()line2 = f.readline()print("Line 1:", line1)print("Line 2:", line2)f.close()
![Page 12: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/12.jpg)
Reading in a Loop• The following pattern is very important• Read all lines in a loop
import iof = open("hello.txt")
l = f.readline()while len(l) > 0 : print(l) l = f.readline()
f.close()
Read first lineStop when line
blankRead next line at end of loop
![Page 13: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/13.jpg)
End of the file?
• In Python• f.readline() gives an empty string at the end of
a file• What about blank lines?• Blanks lines are NOT empty: a blank line
contains a single "\n" character• Note: "\n" is a line separator; there may not be one at
the end of the last line
• Other languages use different methods to signal the end of file
![Page 14: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/14.jpg)
DESIGN A FILE
![Page 15: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/15.jpg)
Problem: Phone Numbers
• Phone numbers are held in a file• Name• Number
• A program reads the file and searches for the name• Prints the number if name is found
• How should the file be organised?
![Page 16: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/16.jpg)
IdeasAlice,0207001111Bob,0207002222Charlie,0207003333
Alice0207001111Bob0207002222Charlie0207003333
AliceBobCharlie020700111102070022220207003333
![Page 17: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/17.jpg)
Using Split
• Multiple values on a line have to be separated
• 'split' method: string list of strings
Alice,0207001111Bob,0207002222Charlie,0207003333
>>> astring = "Bob,12345,SW13 4NN"
>>> astring.split(",")['Bob', '12345', 'SW13 4NN']
>>> astring.split("3")['Bob,12', '45,SW1', ' 4NN']>>>
![Page 18: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/18.jpg)
Reading Numbers
• We use split to discard the "\n" character at the end of lines containing a number
f = open("numFile.txt", 'r')line1 = f.readline()f.close()
num = line1.split("\n")[0]print("Double the number is", int(num)*2)
![Page 19: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/19.jpg)
TEACHING ISSUES
![Page 20: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/20.jpg)
Issues for I/O
• Must understand representation: • e.g. number versus text• e.g. lines
• File operation are object-based• Open returns an object• Do operations on the object
![Page 21: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/21.jpg)
Issues for I/O
• Input is difficult:• How many lines?• What is on the lines?
• Design file contents so it is easy to read
• Error handling• E.g. file name wrong• Needs 'exceptions'
![Page 22: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/22.jpg)
SYLLABUS
![Page 23: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/23.jpg)
2.3.1 Programming techniquesCandidates should be able to:(a) identify and use variables, operators, inputs, outputs and
assignments(b) understand and use the three basic programming constructs used
to control the flow of a program: Sequence; Conditionals; Iteration
(c) understand and use suitable loops including count and condition controlled loops
(d) use different types of data including Boolean, string, integer and real appropriately in solutions to problems
(e) understand and use basic string manipulation(f) understand and use basic file handling
operations: open, read, write and close (g) define and use arrays as appropriate when solving problems.
![Page 24: Programming for GCSE Topic 10.1: Files](https://reader035.vdocuments.us/reader035/viewer/2022062218/568163ac550346895dd4c167/html5/thumbnails/24.jpg)
Summary
• Files• Small number of operations• Combines other aspects of programming: • Loops • Representation: including lines
• Input challenge• Some issues• Object-based library