File IO and Strings CIS 40 – Introduction to Programming in Python

Slides:



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

CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
CS0007: Introduction to Computer Programming File IO and Recursion.
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.
Course A201: Introduction to Programming 12/9/2010.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Intro Python: Variables, Indexing, Numbers, Strings.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Storing and Retrieving Data
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.
5 1 Data Files CGI/Perl Programming By Diane Zak.
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 ….
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
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])
CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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 I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Computers and Programming
Chapter 14: Sequential Access Files
Development Environment
Topic: File Input/Output (I/O)
Graphics CIS 40 – Introduction to Programming in Python
Fundamentals of Python: First Programs
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
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.
Introduction to Computing Science and Programming I
Containers and Lists CIS 40 – Introduction to Programming in Python
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python’s input and output
Variables, Expressions, and IO
Managing results files
Functions CIS 40 – Introduction to Programming in Python
Ruth Anderson UW CSE 160 Winter 2016
Engineering Innovation Center
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 13: File Input and Output
CS190/295 Programming in Python for Life Sciences: Lecture 1
Ruth Anderson UW CSE 160 Winter 2017
Selection CIS 40 – Introduction to Programming in Python
Python I/O.
Ruth Anderson UW CSE 160 Spring 2018
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Fundamentals of Programming I Files
Learning to Program in Python
Exceptions and files Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Topics Introduction Hardware and Software How Computers Store Data
Fundamentals of Data Structures
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Loops CIS 40 – Introduction to Programming in Python
Introduction to Python: Day Three
Using Text Files in Python
Lesson 08: Files Class Chat: Attendance: Participation
Python programming exercise
Files Handling In today’s lesson we will look at:
Fundamentals of Python: First Programs
15-110: Principles of Computing
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Topics Introduction to File Input and Output
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

File IO and Strings CIS 40 – Introduction to Programming in Python De Anza College Clare Nguyen

Intro Data that we store in variables are saved (or kept) only while the script or the code is running. As soon as the code stops running, the data and the code are no longer available. If we want the data in our code to be kept and accessed even after the code has stopped running, then we need to store them in a file. A file is a block of data that has a name and is saved in secondary memory. Secondary memory can be physically close by such as a hard disk or a flash drive (USB drive), or it can be located farther away but is connected to our computer through the network or the internet. These include servers (computers) or the cloud (also servers). In this module we use instructions that will write data to and read data from a text file that is on a disk. This way we can save data between program runs.

Accessing Files This diagram shows how our code interacts with a text file. We call the functions open, read, write, and close to access the text file through a file handle in these steps: Call the function open to ask for a file handle. If the file open is successful, we’re given a file handle. Call the read function or write function to access data in the text file through the file handle. When done, close the file handle. our code file handle open read write close Text file

File Open (1 of 2) To open or request a file handle: handle_name = open(“filename”) open will search for a file with the given filename and check that we have permission to access the file. If we can access the file, a file handle is created and we store it in the variable handle_name. If we cannot access the file, open will cause an exception, and we can write exception handling code for it. Example: variable for file handle file name If the file is not in the same folder (or directory) as the code, add a file path also.

File Open (2 of 2) There is a small difference when calling open for reading a file vs. writing a file. Open for reading: handle_name = open(“filename”) Reading means data goes from the text file into our code. Open for writing: handle_name = open(“filename”, “w”) Writing means data goes from our code into the file. If the file already exists, the file will be overwritten with new data. If the file doesn’t exist, a new file will be created. Open for appending: handle_name = open(“filename”, “a”) Appending means data also goes from our code into the file. If the file already exists, new data will be appended or added to the end of the existing data. If the file doesn’t exist, a new file will be created.

Read From File (1 of 2) There are 2 ways to read from a text file: read one line at a time or read the entire file. Read one line of a time: for line in file_handle The file_handle acts as a list of lines from the file. The for loop means that one line of the file goes into the “line” variable with each loop iteration. Example: With each iteration of the for loop, one line of the file is read in and stored in the “line” variable. The line from file is read in with its newline character at the end of the line. Therefore, when we print, we need to tell the print function not to print with the default newline.

Read From File (2 of 2) Read the entire file: file_input = file_handle.read() file_input is a string variable (data type str). The file_handle has a read function which reads in all lines of the file into a string. Example of reading all lines of the file and printing them: Read in the entire file only with small files. With large files, read in one line at a time.

Write To File There are 2 ways to write to text file: overwrite existing data or append to existing data. The 2 modes are chosen with the file open function. The write function is the same for both modes. Write one line of text to the file: file_handle.write(“text\n”) The file_handle has a write function that writes “text” to the file. The write function doesn’t add the newline character at the end of the line, so if you want the text to be a single line of text in the file, add the newline (\n) character. Example:

Close File When finishing reading or writing the file, close the file handle. Format: file_handle.close() If we forget to close the file handle, the computer will eventually close it for us. However, it’s not considered good practice to leave a file handle opened when it’s not needed. If we don’t close the file handle, the text file cannot be opened by another program that needs to access it.

Click for a video demo of file IO.

String Operations Since the data in the text file are strings, we review some string operators and learn some common string functions. Review: A string is a sequence of characters. str1 + str2 means str1 and str2 are joined end to end. str1 * 4 means str1 is repeated 4 times, joined end to end. A newline character is \n and causes the print function to move to the next line of output. Therefore, the string “a\nb\nc\n” has 6 characters and will print 4 lines of text: a, then b, then c, then a blank line.

String Functions New string operator: str1 == str2 returns True if str1 and str2 are identical (every character is the same). A few new string functions: len(str1) returns the total number of characters in str1 str1.rstrip() returns str1 but without the newline character at the end str1.find(“text”) returns -1 if “text” is not found in str1 returns the position of “text” in str1 if found There are many other string functions that are useful to work with text strings. They are covered in the next Python course.

Strings and the in Operator A substring is a part of a string. Example: “all” and “low” are both substrings of “swallow” The in operator returns a Boolean to indicate whether a string is a substring of a larger string. Example: When used with a for loop, the in operator separates each character of the string into the iteration variable. Code Output

What’s Next We learned some basic operations and functions to work with text strings because our code can access data in text files. Having our code save data into a file or read data from a file means that we can save data in between program runs. Being able to access a file from our code is also important when we need to work with a large number of data values, since files can store much more data than main memory, where our code resides during run time. Now that we can write code to access a large number of data values, in the next module we will have an overview of how Python can help us organize the data into collections or meaningful groups of data.