TeachingLondon Computing

TeachingLondon Computing
Slide Note
Embed
Share

In this instructional content, students will learn how to develop basic programs for text input/output operations in files. The focus is on understanding file structure and designing a file format. Exploring file I/O operations, asymmetry between input and output, and the concept of 'buffering' are key topics covered. Basic file operations such as opening, writing, reading, and closing files are demonstrated through example programs. The significance of file buffering and representing data in files as sequences of bytes is explained.

  • Programming
  • File Operations
  • Text I/O
  • File Format
  • Basic Operations

Uploaded on Feb 26, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. TeachingLondon Computing Programming for GCSE Topic 10.1: Files William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

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

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

  4. BASIC FILE OPERATIONS

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

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

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

  8. EXAMPLE PROGRAMS

  9. 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 line This is a string followed by more on the line

  10. 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()

  11. 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()

  12. Reading in a Loop The following pattern is very important Read all lines in a loop import io f = open("hello.txt") Read first line l = f.readline() while len(l) > 0 : print(l) l = f.readline() Stop when line blank Read next line at end of loop f.close()

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

  14. DESIGN A FILE

  15. 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?

  16. Ideas Alice,0207001111 Bob,0207002222 Charlie,0207003333 Alice 0207001111 Bob 0207002222 Charlie 0207003333 Alice Bob Charlie 0207001111 0207002222 0207003333

  17. Alice,0207001111 Bob,0207002222 Charlie,0207003333 Using Split Multiple values on a line have to be separated 'split' method: string list of strings >>> astring = "Bob,12345,SW13 4NN" >>> astring.split(",") ['Bob', '12345', 'SW13 4NN'] >>> astring.split("3") ['Bob,12', '45,SW1', ' 4NN'] >>>

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

  19. TEACHING ISSUES

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

  21. 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'

  22. SYLLABUS

  23. 2.3.1 Programming techniques Candidates 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.

  24. Summary Files Small number of operations Combines other aspects of programming: Loops Representation: including lines Input challenge Some issues Object-based library

Related


More Related Content