And now for something completely different . . .

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

COMPUTER PROGRAMMING Task 1 LEVEL 6 PROGRAMMING: Be able to use a text based language like Python and JavaScript & correctly use procedures and functions.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Computer Science 111 Fundamentals of Programming I Files.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Python and Web Programming
Topics This week: File input and output Python Programming, 2/e 1.
CS0007: Introduction to Computer Programming File IO and Recursion.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Python – reading and writing files. ??? ???two ways to open a file open and file ??? How to write to relative and absolute paths?
JSON COMPSCI 105 SS 2015 Principles of Computer Science.
BASIC WORD PROCESSORS WEEK 5. BASIC WORD PROCESSORS Word Processor Word processor is a program which is used to edit text files and format them with font,
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])
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
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.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Getting Started With Python Brendan Routledge
Xi Wang Yang Zhang. 1. Strings 2. Text Files 3. Exceptions 4. Classes  Refer to basics 2.py for the example codes for this section.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Introducing Python Introduction to Python.
Topic: File Input/Output (I/O)
Introduction to Programming
Fundamentals of Python: First Programs
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
What Actions Do We Have Part 1
Example: Vehicles What attributes do objects of Sedan have?
Lesson 1 An Introduction
Introduction to Programming
IST256 : Applications Programming for Information Systems
Computer Science 112 Fundamentals of Programming II GUIs II:
And now for something completely different . . .
Introduction to Programming
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
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.
Fundamentals of Programming I Files
And now for something completely different . . .
Loop Structures and Booleans Zelle - Chapter 8
The backslash is used to escape characters that are used in Python
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Using Text Files in Python
Input and Output with FILES
Introduction to Programming
Lesson 08: Files Class Chat: Attendance: Participation
Fundamentals of Python: First Programs
3.1 Basic Concept of Directory and Sub-directory
15-110: Principles of Computing
Appending or adding to a file using python
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Introduction to Programming
Introduction to Computer Science
Introduction to Python
Topics Introduction to File Input and Output
Files and Dictionaries
Python Reserved Words Poster
Presentation transcript:

And now for something completely different . . . Jan 5, 2018 AD c 2019

Part 9 Python 3 Files Nov 10, 2017 AD c 2019

Python Files 9.1 Introduction to Files 9.2 File Operations Jan 5, 2018 AD c 2019

Python Files 9.1 Introduction to Files 9.2 File Operations Jan 5, 2018 AD c 2019

Introduction to Files Python has a file object type which permits the use of file methods, allowing access to and manipulation of files which are stored on the computer disk. Jan 5, 2018 AD c 2019

Text Files A simple text file can be created using a text processing application such as Window's Notepad, Window's Wordpad or a word processor such as Microsoft Word. Jan 5, 2018 AD c 2019

Jan 5, 2018 AD c 2019

Jan 5, 2018 AD c 2019

Jan 5, 2018 AD c 2019

Jan 5, 2018 AD c 2019

Jan 5, 2018 AD c 2019

Python Files 9.1 Introduction to Files 9.2 File Operations Jan 5, 2018 AD c 2019

An example program using a file This program reads a line from the file C:\temp\file1.txt, then prints out the line. file1 = open('C:\\temp\\file1.txt','r') # the line above opens C:\temp\file1.txt for reading string = file1.readline() print (string) The output: Python is a very good http://www.annedawson.net/python3programs.html09 -01.py Jan 5, 2018 AD c 2019

Video: How to read a file in Python https://youtu.be/KA-DFjpYFiM Jan 5, 2018 AD c 2019

Creating and writing to a file This program creates a file called tester2.txt in folder C:\temp, writes two lines to it, then closes the file. file1 = open("C:\\temp\\tester2.txt","w") print (file1) # prints out details about the file file1.write("Today is Monday\n") file1.write("Tomorrow is Tuesday") file1.close() http://www.annedawson.net/python3programs.html09 -02.py Jan 5, 2018 AD c 2019

Video: How to write to a file in Python https://youtu.be/Us_tkZbMk4M Jan 5, 2018 AD c 2019

Video: Using the backslash escape sequence https://youtu.be/Stc4k4CG-zM Jan 5, 2018 AD c 2019

Reading data from a file This program opens the file called tester2.txt in folder C:\temp, reads the file into a string variable and prints the string. The file is then closed and opened again, and this time five characters at a time are read from the file. file2 = open("C:\\temp\\tester2.txt","r") print (file2) # prints out details about the file string1 = file2.read() print (string1) file2.close() string1 = file2.read(5) http://www.annedawson.net/python3programs.html09 -03.py Jan 5, 2018 AD c 2019

Copying files This program makes an exact copy of a file. It uses a function to perform the processing. def copyFile(oldFile, newFile): f1 = open(oldFile, "r") f2 = open(newFile, "w") while 1: text = f1.read(50) if text == "": break f2.write(text) f1.close() f2.close() return filecopy = "C:\\temp\\tester2copy.txt" #this file will be created fileold = "C:\\temp\\tester2.txt" # existing file copyFile(fileold, filecopy) http://www.annedawson.net /python3programs.html09 -04.py Jan 5, 2018 AD c 2019

Handling the “file not found” exception This program opens a file if it exists, otherwise prints an error message. filename = input('Enter a file name: ') try: f = open (filename, "r") except: print ('There is no file named', filename ) http://www.annedawson.net/python3programs.html09 -05.py Jan 5, 2018 AD c 2019

Video: Python Errors and Exceptions https://youtu.be/mpYFIwYYD9A Jan 5, 2018 AD c 2019

Video: Python Exception Handling https://youtu.be/zXINeVywNZU Jan 5, 2018 AD c 2019

Video: Python File Searching https://youtu.be/-VvhBKhY-Z4 Jan 5, 2018 AD c 2019

This Presentation uses the following program files: 09-01.py 09-02.py 09-03.py 09-04.py 09-05.py see all programs at: http://www.annedawson.net/python3programs.html Jan 5, 2018 AD c 2019

This Presentation uses the following videos: https://youtu.be/KA-DFjpYFiM https://youtu.be/Us_tkZbMk4M https://youtu.be/mpYFIwYYD9A https://youtu.be/8SGzhngNTzM https://youtu.be/Stc4k4CG-zM https://youtu.be/zXINeVywNZU https://youtu.be/-VvhBKhY-Z4 see all videos at:https://www.youtube.com/user/annedawson/videos Jan 5, 2018 AD c 2019

Last updated: Friday 5th January 2018, 6:15 PT, AD AD c 2019