Bryan Burlingame 17 April 2019

Slides:



Advertisements
Similar presentations
The Windows File System and Windows Explorer To move around the file system and examine your files or get to one you want (say, to modify, delete or copy.
Advertisements

Drives, Directories and Files. A computer file is a block of arbitrary information, or resource for storing information. Computer files can be considered.
Computer Science 111 Fundamentals of Programming I Files.
File System Interface CSCI 444/544 Operating Systems Fall 2008.
Guide To UNIX Using Linux Third Edition
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Computer Systems Week 10: File Organisation Alma Whitfield.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
File Systems (1). Readings r Reading: Disks, disk scheduling (3.7 of textbook; “How Stuff Works”) r Reading: File System Implementation ( of textbook)
Managing Files. Module 5 Managing Files ♦ Introduction “On a Linux system, everything is a file; if something is not a file, it is a process.” ♦ Topics.
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.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
PC204 Lecture 5 Conrad Huang Genentech Hall, N453A
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 3 Windows File Management 1 Morrison / Wells / Ruffolo.
ITFN 2601 Introduction to Operating Systems Lecture 22 Files & Directories.
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
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.
File System Interface CSSE 332 Operating Systems
Topic: File Input/Output (I/O)
COMP Streams and File I/O
Fundamentals of Python: First Programs
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
File I/O File input/output Iterate through a file using for
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Naming and Saving Files
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
University of Central Florida COP 3330 Object Oriented Programming
Binary Files.
Creating and Modifying Text part 2
Ruth Anderson UW CSE 160 Winter 2016
Exceptions and files Taken from notes by Dr. Neil Moore
Bryan Burlingame 26 September 2018
Chapter 13: File Input and Output
Operation System Program 4
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Spring 2018
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
Lecture 5 Fruitful Functions
Lecture 2 Python Programming & Data Types
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.
Bryan Burlingame 17 October 2018
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
Exceptions and files Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
Bryan Burlingame 28 November 2018
Lecture 2 Python Programming & Data Types
Lesson 08: Files Class Chat: Attendance: Participation
Strings and Serialization
Fundamentals of Python: First Programs
Bryan Burlingame 6 March 2019
Topics Introduction to File Input and Output
Bryan Burlingame 13 March 2019
Bryan Burlingame Halloween 2018
Bryan Burlingame 24 April 2019
Lecture 13 Teamwork Bryan Burlingame 1 May 2019.
Topics Introduction to File Input and Output
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
Presentation transcript:

Bryan Burlingame 17 April 2019 Lecture 11 Files Bryan Burlingame 17 April 2019

Announcements Read ch. 8, 9, 10, 11 in Automate the Boring Stuff Lab 10 should be individual effort. It will be presented to me next week for code review.

Learning Objectives Introduce data persistence Discuss file operations Introduce exceptions try and except Present simple databases Introduce pickling Discuss writing a module

Data Persistence So far, we’ve dealt with manual input Some user has had to type in the data each time the program is run Think of the music program. Typing in the music each time is flawed. We need methods to store data across program executions Data persistence is storing data in some permanent manner to be used by a program over long periods of time Today will talk about two: files and databases

Text Files To write to a file use the write method of the file object A text file is a sequence of characters read from a permanent storage device Files are referred to by a file name and reside at a location on the storage device Ex: c:\files\input.txt, a file called input.txt resides on a drive with the letter c in the path \files\ To open a file for write, use open with the ‘w’ parameter Ex: fout = open(“c:\files\output.txt”, “w”) To write to a file use the write method of the file object Ex: fout.write(“Hello world”) Note: write only accepts strings

Filenames and Paths Files are organized into directories (sometimes called folders) Directories are a tree built off of some root (called the root directory) C:\ is the root directory of drive C: Absolute path describes the path in relation to the root of the system C:\files\inputs\world.txt is the absolute path to the world.txt file residing in a directory inputs which is a subdirectory of files which is a subdirectory of the root of C: Every program runs within some current working directory (usually the directory in which the program resides) If the program’s path is c:\programs\my_program\my_program.py, then the current working directory at program launch is usually c:\programs\my_program A relative path is a path to a file relative to the current working directory If there was a file in c:\programs\my_program\inputs\input.txt, that file could be referred to by inputs\input.txt. This path is related to the current working directory

Filenames and Paths The os object contains many useful methods import os os.getcwd() – returns the current working directory as a string os.path.exists(‘path\filename’) – returns true if a file or directory exists os.path.isdir(‘path’) – returns true if the path exists and is a directory (use with exists to determine if something is a file or a directory) os.path.abspath(‘relative_path\filename’) – returns the absolute path to a file os.listdir(‘directory) – returns a list of the files located in that directory

Catching Exceptions Previously, if we had a runtime error, what happened?

Catching Exceptions BOOM! Previously, if we had a runtime error, what happened?

Catching Exceptions Previously, if we had a runtime error, what happened? There’s a way to catch the error and keep control try: except: The program will try to run what is in the try block, if there is an error control will be handed to the except block This allows the programmer to clean up after errors

Catching Exceptions Since a system can change outside the control of a program, putting file operations in a try block is preferred We’ll revisit this topic in future conversations

Database A database is a file specifically organized for storing data The dbm module gives a method to store key, value pairs on disk Very much like a dictionary, but persistent dbm.open(‘db_name’, ‘c’) opens a database for read/write, creating it if it doesn’t exist ‘w’ opens an existing database for read/write ‘r’ opens an existing database for read-only (default) ‘n’ opens a new empty database

Database A database is a file specifically organized for storing data The dbm module gives a method to store key, value pairs on disk Very much like a dictionary, but persistent

Database A database is a file specifically organized for storing data The dbm module gives a method to store key, value pairs on disk Very much like a dictionary, but persistent Values can only be strings or bytes

Pickling There is a module, pickle which can work around this limitation Pickling a value converts it to a string in a way that it can be stored in a database and then unpickled to it original value Use the dumps method to pickle and loads to unpickle

Pickling There is a module, pickle which can work around this limitation Pickling a value converts it to a string in a way that it can be stored in a database and then unpickled to it original value Use the dumps method to pickle and loads to unpickle This works on the containers we’ve already discussed

Traversing a dbm Database The for construct can be used to traverse a dbm database just as it can for a dictionary There is one caveat Keys are stored as byte objects, not strings

Traversing a dbm Database The for construct can be used to traverse a dbm database just as it can for a dictionary There is one caveat Keys are stored as byte objects, not strings We could try the str function. It kinda’ works

Traversing a dbm Database The for construct can be used to traverse a dbm database just as it can for a dictionary There is one caveat Keys are stored as byte objects, not strings We could try the str function. It kinda’ works To convert a byte object to a string, use the decode method with utf-8 as the decode type

Resources Downey, A. (2016) Think Python, Second Edition Sebastopol, CA: O’Reilly Media (n.d.). 3.7.0 Documentation. 5. Data Structures — Python 3.7.0 documentation. Retrieved October 30, 2018, from https://docs.python.org/3/tutorial/datastructures.html