File Handling.

Slides:



Advertisements
Similar presentations
Computer Science 111 Fundamentals of Programming I Files.
Advertisements

Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Why use Files? Your Python Program External File (secondary storage) Write to file (Save) Read from file (Load) …so we can have access to ‘stored’ data.
Scripting CBIS BASH Scripting Step 1 – Create the bash file. Usually a good idea to end it in.sh file1.sh Step 2 – Using CHMOD make the bash file.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
File I/O Michael Ernst UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Files Tutor: You will need ….
File Handling in QBASIC
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
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.
File I/O Ruth Anderson UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
QUIZ MODULE. You can Add the quiz title or heading Select the to and form date for the quiz Description of quiz Prize being offered – If you have any.
Getting Started With Python Brendan Routledge
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
Python: File Management Damian Gordon. File Management We’ve seen a range of variable types: – Integer Variables – Real Variables – Character Variables.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
Starter What does the following code do?
GCSE COMPUTER SCIENCE Practical Programming using Python
Writing algorithms Introduction to Python.
Topic: File Input/Output (I/O)
Introduction to Programming
Writing & reading txt files with python 3
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Introduction to Programming
IST256 : Applications Programming for Information Systems
Lesson 3 - Repetition.
Managing results files
Functions CIS 40 – Introduction to Programming in Python
Ruth Anderson UW CSE 160 Winter 2016
Ruth Anderson UW CSE 160 Winter 2017
Example Programs.
Introduction to Programming
Python I/O.
Ruth Anderson UW CSE 160 Spring 2018
File Handling Programming Guides.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Fundamentals of Programming I Files
HOW TO CREATE A CLASS Steps:
Introduction to TouchDevelop
Introduction to Programming
Introduction to Programming
Text / Serial / Sequential Files
Introduction to Programming
How to save information in files open, write, close
3.1 Basic Concept of Directory and Sub-directory
Python Basics with Jupyter Notebook
CSCI N207 Data Analysis Using Spreadsheet
DAT2343 LMC Simulator Usage © Alan T. Pinck / Algonquin College; 2003.
Introduction to Programming
Small Basic Programming
Introduction to Python
Introduction to Programming
Java Lessons Mr. Kalmes.
Hint idea 2 Split into shorter tasks like this.
Introduction to Programming
Dry Run Fix it Write a program
Starter Which of these inventions is: Used most by people in Britain
What you need to do… Drag the pieces of code into the correct order like you can see in the EXAMPLE below. print ( “ int input ) = + Hello world chr ord.
File Handling.
GCSE Computing.
GCSE Computing.
Presentation transcript:

File Handling

File Handling One of the issues with using variables to store data is that as soon as the program is closed the contents (e.g. a password) are deleted and reset once the program is re-opened. That would not be suitable in the real world so we must write data to files that could then be recalled at a later date.

File Handling – writing to file ‘wt’ #Write the following code and save to your area. print(“Below is the code to write data to a text file\n”) #writing to file myFile = open('details.txt', 'wt') myFile.write ('I have written this data to file\n') myFile.write ('I have written this data to file again\n') myFile.write ('I have written this data to file yet again!\n') myFile.close()

File Handling – writing to file ‘wt’ #Run the code and then close the program, there should now be a details.txt file stored in the same directory as your python code. Re-open and add the code below: print("Congratulations you have written three lines of text to file!") print("This is stored as a text file in the same folder as this program") print("Now to read from the text file so that the details can be displayed in this code")

File Handling – reading from file ‘rt’ #enter the code below: #reads contents, stores in a variable then prints myFile = open('details.txt', 'rt') contents = myFile.read() #stores all details from text file in contents variable print(contents) myFile.close()

File Handling – reading from file ‘rt’ #enter the code below: print("\n That's great but it reads all of the contents together and then outputs to screen") print("What if we want to read a line at a time?")

File Handling – reading from file ‘rt’ #enter the code below: #reading contents a line at a time myFile = open('details.txt', 'rt') for line in myFile: #for loop to read a line at a time print(line) print("\nAnd the next line has...") myFile.close()

File Handling – Applying skills Write a program that reads a list of numbers from a file then outputs the average. So if your file contained: 3 45 83 21 Your program would output: 38 Hints: myFile.write ('3\n'), etc. total = total + int(line) #to add the numbers #more code required

File Handling – appending to file ‘a’ #enter the code below: myFile = open("details.txt", "a") myFile.write("appended text") myFile.close() #reads contents, stores in a variable then prints myFile = open('details.txt', 'rt') contents = myFile.read() print(contents)

File Handling – developments to your code #Create a Menu using print (and an input) for the following: ****File Handling**** 1…..Write to file 2…..Read data from file 3…..Append data to file Please enter selection: _ #Use procedures for each option and an if elif to call the correct procedure depending on the user’s selection.