File Handling Programming Guides.

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

Lists Introduction to Computing Science and Programming I.
Debugging Introduction to Computing Science and Programming I.
Python and Web Programming
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
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.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
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 ….
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.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
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])
Unit 6 Repetition Processing Instructor: Brent Presley.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
String and Lists Dr. José M. Reyes Álamo.
Unit 2 Technology Systems
MOM! Phineas and Ferb are … Aims:
Whatcha doin'? Aims: To start using Python. To understand loops.
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Module 5 Working with Data
Tuples and Lists.
Introduction To Repetition The for loop
IF statements.
CS 115 Lecture 8 Structured Programming; for loops
Error Handling Summary of the next few pages: Error Handling Cursors.
Engineering Innovation Center
Starter Write a program that asks the user if it is raining today.
Programming – Touch Sensors
While Loops BIS1523 – Lecture 12.
Python I/O.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Number and String Operations
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Data Structures – 1D Lists
Fundamentals of Data Structures
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Introduction to TouchDevelop
Debugging at Scale.
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Inputs and Variables Programming Guides.
Files Handling In today’s lesson we will look at:
Random Access Files / Direct Access Files
Flowcharts and Pseudo Code
Data Structures & Algorithms
Introduction to Computer Science
Making decisions with code
Introduction to Computer Science
Introduction to Python
Topics Introduction to File Input and Output
Starter Activities GCSE Python.
Class code for pythonroom.com cchsp2cs
Python Simple file reading
How to read from a file read, readline, reader
Introduction to Computer Science
Introduction to Computer Science
Presentation transcript:

File Handling Programming Guides

File Handling Text Files and Python In python, like all other languages, it is possible to store in text files data held within the program (such as data in variables and lists). This is great as is means that when our program closes, we can still keep our data.

File Handling Creating the file and opening it in write mode Before we can write data into a text file we first need to create the text file. We need to use a new function, called ‘open()’. This creates the file and allows us to write text to it. Firstly, we create a variable (called a file handler) that python will use to identify the text file, then we use the open function to assign the external file to it. We type in the name of the new file, and what type it is. For this guide we will be using text files. Text files are designated as .txt. We also need to tell the computer that we will be writing to the new file. This is why we have the ,”w” after the file name and file type. The ‘w’ means ‘write’.

File Handling Writing to the File To start writing to the file we have created we need to add ‘.write’ to the end of our new variable followed by brackets. In this brackets we add our text. If we are going to write more than one line to the file, we need to tell the computer when to start a new line. To do this, we use ‘\n’ at the end of each line. We don’t need to put this in for the last line because there is no other line coming after it.

File Handling Closing the File We need to close the file after we have written to it otherwise we will not be able to see it once we are finished. All we have to do to close the file is add ‘.close()’ to the end of our file handler variable.

File Handling The Result The file that is created and written to will be in the same folder as the program that you made to create the text file. ()

File Handling Reading a File Opening a file in read mode Reading a file in python is even easier than writing to a file. We first need to open the file we are using in read mode. To be able to read a file you need to have created one beforehand. We do this by typing in the file name and file type, followed by a comma and “r”. This tells the computer that we will be opening the file in order to read its contents. In this example I am using the same file that I wrote to earlier on.

File Handling Reading the File All we have to do to actually see the text within the file is print the text file’s variable with .read() attached to the end of the file hander’s name.

File Handling The Result Here is the code and the resulting program.

File Handling Reading Only One Line at a Time To read only one line at a time we use ‘.readline()’ instead of .read()

File Handling Reading Only Specific Lines To read specific lines we use ‘.readlines()’ followed by the line number in square brackets.

File Handling Problems from opening a file in Python Sometimes, if we don’t close a file that we have opened in our program, errors can occur. So, there is another method of opening a file which could be considered superior because with this method we don’t have to remember to close the file when we are done with it. This ‘better’ method is done using the ‘with’ statement.

File Handling Reading an entire file and storing it in a variable (as a string) If you have a text file stored in the same folder as your program… …you can open it up in your program using a ‘with open’ statement. You need to: add the name of the file state what MODE you wish to open the file in (read mode (r) or write mode (w)) Give a name for a variable which is to hold the file in program Once it is open, if you use the ‘read()’ function on the file handler variable (a_file), you can read the contents of the file and later print it to the screen

File Handling Reading a particular line and storing it in a variable (as a string) If you wished to only read a specific line of the file you can specify the line using the function ‘readlines()’ followed by the line number in square brackets.

File Handling Reading a each line in turn and storing it in a variable (as a string) If you wished to read each line, one after the other, you can set up a little FOR loop which will loop for each line in the file. Here you can see how each line is read, stored in a variable and then printed within each iteration.

File Handling Printing each Line in Turn (without the blank line between each item) Adding end=“”, to the end of the print statement will ensure that it will not add a blank line between the printed lines from the file.

File Handling Finding and Printing Lines based on ‘Set Criteria’

File Handling Reading a each line in turn BUT ONLY storing it in a variable when it matches search criteria This is an example of searching for a specific item within a line of a file. It is simply carried out by asking for some search criteria (in this case ‘name’) and then using a simple IF statement to see if the ‘name’ is in each line.

File Handling Another way of reading a each line in turn BUT ONLY storing it in a variable when it matches search criteria This is another example of searching for a specific item within a line of a file. This works the same as the last but as you can see it is a little more tidy. It uses a flag variable which starts off as FALSE and only changes to TRUE if the search is successful. If it is not successful then a final IF statement will say that not item was matched.

File Handling Update / Overwrite Here is an example of reading the entire text file into a list… …then overwriting a specific line (list item) with new data… …then writing this list back to the text file.

File Handling Delete Line Here, the entire text file is read into a list, then a particular list item is popped out. Finally the list is written back to the file.

File Handling Insert – at a specific line number Here, the text file is being read into a list… …then new data is being inserted into a new position of the list… …then the list is being written back to file.