Introduction to Computer Science Programming with Python Files



























- Slides: 27

Introduction to Computer Science Programming with Python

Files – I/O Chapter 7

File Processing A text file can be thought of as a sequence of lines http: //www. py 4 e. com/code/mbox-short. txt

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

Using open()

What is a Handle?

Missing Files

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

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

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

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

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

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

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

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?

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=“”)

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

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

Prompt for File Name

Error Handling

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

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

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: http: //www. pythonlearn. com/code 3/mbox-short. txt ▪ Executing the program will look as follows:

Sample Solution - Read

Read, then write to a new file

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)

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