Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science

Similar presentations


Presentation on theme: "Introduction to Computer Science"— Presentation transcript:

1 Introduction to Computer Science
Programming with Python

2 Files – I/O Chapter 7

3 File Processing A text file can be thought of as a sequence of lines

4 Open a File Before we can read the contents of a file, we must tell Python which files we are going to work with and what we will be doing with the file This is done with the open() function open() returns a “file handle” - a variable used to perform operations on the file Similar to file -> open in a Word Processor

5 Using open()

6 What is a Handle?

7 Missing Files

8 The newline Character We use a special character called the “newline” to indicate when a line ends We represent it as \n in strings The newline is still one character – not two

9 File Processing A text file has newline at the end of each line – things you do not see but exist

10 File Handle as a Sequence
A file handle open for read can be treated as a sequence of strings where each line in the file is a string in the sequence We can use for statement to iterate through a sequence Remember, a sequence is an ordered set

11 Counting Lines in a File
Open a file read-only Use a for loop to read each line Count the lines and print out the number of lines

12 Reading the “Whole” File
We can read the whole file (newlines and all) into a single string

13 Searching Through a File
We can use if statement in our for loop to only print lines that meet some criteria

14 Blank Lines What are all these blank lines?
Each line from the file has a newline (\n) at the end The print statement adds a newline to each line

15 Blank Lines What are all these blank lines?
Each line from the file has a newline (\n) at the end The print statement adds a newline to each line So, how do we deal with this?

16 Searching Through a File (fixed)
We can strip the whitespaces from the right-hand side of the string using rstrip() from the string library The newline is considered “white space” and is stripped rstrip() will remove the new line from the text file OR, print(line, end=“”)

17 Skipping with Continue
We can conveniently skip a line by using the continue statement

18 Using in to Select lines
We can look for a string anywhere in a line as our selection criteria

19 Prompt for File Name

20 Error Handling

21 With open With open will close automatically after finishing all lines of code in the function Closing is necessary when writing to files

22 Mode "a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content “r” – Read - will ready from file

23 Exercise 7.1 – from the book
Exercise 1: Write a program to read through a file and print the contents of the file (line by line) all in upper case. You can download the file from: Executing the program will look as follows:

24 Sample Solution - Read

25 Read, then write to a new file

26 Read CSV Instead of reading a comma separated value (CSV) file as a text file, then split it by comma in order to get one specific column, we can simply read CSV which will split every row by comma and return a list that contains every element/column of the row import csv student_details = [] with open(file, mode='r') as infile: # print('reading') reader = csv.reader(infile) header = next(reader) for rows in reader: student_details.append(rows)

27 Summary Secondary storage Opening a file – file handle
File structure – newline character Reading a file line by line with a for loop Searching for lines Reading file names Dealing with bad files


Download ppt "Introduction to Computer Science"

Similar presentations


Ads by Google