Writing & reading txt files with python 3

Slides:



Advertisements
Similar presentations
FM Web Scraping FMPUG: Dallas Chapter Taylor Made Services: FileMaker Presentation March 6, 2009 Dallas Texas.
Advertisements

Instructions 1. Open xCode 2. File -> New Project 3. Start a new View based iPhone Project. The type of project you create really.
Computing Theory: BBC Basic Coding Year 11. Lesson Objective You will: Be able to define what BBC basic is Be able to annotate BBC basic code Be able.
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.
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 Programming Using Variables and input. Objectives We’re learning to use basic knowledge of variables combined with user input. Outcomes Continue.
Introduction to Python Lesson 1 First Program. Learning Outcomes In this lesson the student will: 1.Learn some important facts about PC’s 2.Learn how.
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.
Using Text Edit. Create Folders to Organize a Site  Identify the location where you are storing your Web sites. Ask your teacher for help if needed.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Input, Output and Variables GCSE Computer Science – Python.
GCSE COMPUTER SCIENCE Practical Programming using Python
Topic: File Input/Output (I/O)
Whatcha doin'? Aims: To start using Python. To understand loops.
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
A Playful Introduction to Programming by Jason R. Briggs
Exercise : Write a program that print the final price of purchase at a store where everything costs exactly one dollar. Ask for the number of items purchased.
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Week of 12/12/16 Test Review.
Python Let’s get started!.
Python 14 Mr. Husch.
IST256 : Applications Programming for Information Systems
CSC 108H: Introduction to Computer Programming
Lesson 3 - Repetition.
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
To write a Python program, you first need to open Pyscripter
Matlab Diary Matlab allows you to keep a log or diary of your work.
G7 programing language Teacher / Shamsa Hassan Alhassouni.
Python I/O.
CSc 110, Autumn 2016 Lecture 16: File Input.
Python Mr. Husch.
File Handling Programming Guides.
Use proper case (ie Caps for the beginnings of words)
File IO and Strings CIS 40 – Introduction to Programming in Python
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Adapted from slides by Marty Stepp and Stuart Reges
File Handling.
Learning to Program in Python
Python Lessons 13 & 14 Mr. Kalmes.
Teaching London Computing
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
Changing one data type into another
Class Examples.
Python 21 Mr. Husch.
Lesson 08: Files Class Chat: Attendance: Participation
Python programming exercise
Reference semantics, variables and names
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
Python Lesson’S 1 & 2 Mr. Kalmes.
Python Basics with Jupyter Notebook
CSCI N207 Data Analysis Using Spreadsheet
Programming In Lesson 4.
To understand what arrays are and how to use them
Python Strings.
Input and Output Python3 Beginner #3.
Introduction to Python
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Python Lessons 13 & 14 Mr. Husch.
Python 16 Mr. Husch.
Python 10 Mr. Husch.
Just Basic Lessons Mr. Kalmes.
Hint idea 2 Split into shorter tasks like this.
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.
CSC 221: Introduction to Programming Fall 2018
4.4 – List vs Array Exercise 4.1: Array Variables
Iteration – While Loops
Presentation transcript:

Writing & reading txt files with python 3 Txt Files have to be in the same folder as the program

Writing to file

How to write to a text file Create a suitable variable to store the name of the text file. Use the code write to add text to the file Close the text file This means ‘write’ This means ‘new line’

How to write to a text file name = open("filename", "w") name = open("filename", "a") Try the ‘a’ What does it do? Change this w to a, what happens

Create code to write a list of gas prices in the USA to a file: Donald says……. Create code to write a list of gas prices in the USA to a file: 8.20 3.81 3/21/11 8.08 3.84 3/28/11 8.38 3.92 4/4/11 Call the file ‘GAS PRICES.TXT’

Reading from a file

f = open("hours.txt“, “r”) print(f.read()) Reading Files Name = open("filename") Name.Read() - file's entire contents as a string Name.Readline() - read next line from file as a string name.Readlines() - file's contents as a list of lines f = open("hours.txt“, “r”) print(f.read())

Donald wants you to……. Create code that: Asks the user to enter in a sentence Saves the sentence to a file Create a new text file with the sentence “make merica great again!” Save it as a file called usa.txt Adapt the program to read this file Use string skills to print the last word

print mystring[-1] # print last char print mystring[5:-1] # result 5678 print mystring[:-1] # everything but the last char print mystring[4:] # from position 4 and onward print mystring[8:100] # result 89

Any questions?