OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.

Slides:



Advertisements
Similar presentations
Programming for GCSE Topic 10.2: Designing for File I/O T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Advertisements

Computer Science 111 Fundamentals of Programming I Files.
Chapter 4.2 Binary numbers: Arithmetic
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
CS0007: Introduction to Computer Programming File IO and Recursion.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Chapter 2: Binary logic OCR Computing for GCSE © Hodder Education 2011.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions.
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 12: A few other things.
A Level Computing#BristolMet Session Objectives#U2 S7 MUST understand the difference between an array and record SHOULD be able to estimate the size of.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
OCR GCSE Computing © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 1: Introduction.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Files Tutor: You will need ….
Sequencing The most simple type of program uses sequencing, a set of instructions carried out one after another. Start End Display “Computer” Display “Science”
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
B065: PROGRAMMING WRITING TO CSVFILES. Starter  Quick task:  Write a program which asks for the name and age of 3 students and writes these details.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Chapter 1: Introduction to Computers and Programming.
HTML is the language that allows text and graphics to be displayed as Web pages. It is a set of special codes, called tags, that tells a browser application.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Glencoe Introduction to Web Design Chapter 4 XHTML Basics 1 Review Do you remember the vocabulary terms from this chapter? Use the following slides to.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
The Accumulator Pattern Motivating Example: Suppose that you want to add up (“accumulate”) 1 2 plus 2 2 plus 3 2 plus … plus You could use: total.
Programming with ANSI C ++
Interfacing with a database
Binary Files.
Teaching Computing to GCSE
Teaching Computing to GCSE
File Handling Programming Guides.
Topics Introduction to File Input and Output
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
Fundamentals of Programming I Files
File Handling.
For OCR GCSE Computing Unit 1 - Theory
The backslash is used to escape characters that are used in Python
files Dr. Bhargavi Goswami Department of Computer Science
Data Structures – 1D Lists
Fundamentals of Data Structures
Mini Python Project Lesson 3.
Lists in Python Creating lists.
Introduction to Programming
How to save information in files open, write, close
funCTIONs and Data Import/Export
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
See requirements for practice program on next slide.
Topics Introduction to File Input and Output
Put the title here Here is the sub-title.
Put the title here Here is the sub-title.
Introduction to Python
Introduction to Programming
Topics Introduction to File Input and Output
Programming Techniques :: File Handling
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files

OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 10: Files This section looks at text files. You can also work with other types of files such as binary files in Python. Creating and writing to a text file When working with files, we need to set up a variable to refer to the file. This is known as a file descriptor or file handle. my_file=open('my_file.txt','w') my_file is the handle open() activates the file my_file.txt is the physical file on the disk 'w' means write to file. If the file does not exist it will be created. If it does exist, it will be overwritten.

OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 10: Files Here is some code that uses the file write method. Each item is followed by a new line character (\n) to create separate lines in the file. This will make processing easier later. We close the file after use with the close method.

OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 10: Files You can use any text editor to inspect the file that has been written. The new file will look something like this in Windows:

OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 10: Files Appending data to a file If you want to add data to a text file, using the ‘w’ method is no good; it will overwrite your existing file. Use the ‘a’ (append) method instead.

OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 10: Files Reading from a file As you would expect, we need the read method and use ‘r’ to open it.

OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 10: Files The readline method does just that: it reads one line from the file. Usually you will want more control. Use the readlines (plural) method for that.

OCR Computing GCSE © Hodder Education 2013 Slide 8 Python 10: Files You can specify just one line to output by putting the line number in square brackets. Here, we ask for line 2. This gives you: