Python Programming Files, Lecture notes of Computer Science

Introduction to Python Programming

Typology: Lecture notes

2020/2021

Uploaded on 02/08/2021

aliahmed.98
aliahmed.98 🇵🇰

5

(4)

9 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Programming
Chapter 7 - Files
Python for Everybody
www.py4e.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Python Programming Files and more Lecture notes Computer Science in PDF only on Docsity!

Python Programming

Chapter 7 - Files

Python for Everybody

www.py4e.com

File Processing

A text file can be thought of as a sequence of lines From [email protected] Sat Jan 5 09:14:16 2008 Return-Path: Date: Sat, 5 Jan 2008 09:12:18 - 0500 To: [email protected] From: [email protected] Subject: [sakai] svn commit: r39772 - content/branches/ Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=

http://www.py4e.com/code/mbox-short.txt

Using open()

  • handle = open(filename, mode)
  • returns a handle use to manipulate the file
  • filename is a string
  • mode is optional and should be 'r' if we are planning to read the file and 'w' if we are going to write to the file fhand = open('mbox.txt', 'r')

What is a Handle?

>>> fhand = open('mbox.txt')

>>> print(fhand)

<_io.TextIOWrapper name='mbox.txt' mode='r' encoding='UTF-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
  • Newline is still one character - not two

>>> stuff = 'Hello\nWorld!'

>>> stuff

'Hello\nWorld!'

>>> print(stuff)

Hello

World!

>>> stuff = 'X\nY'

>>> print(stuff)

X

Y

>>> len(stuff)

File Processing

A text file can be thought of as a sequence of lines From [email protected] Sat Jan 5 09:14:16 2008 Return-Path: Date: Sat, 5 Jan 2008 09:12:18 - 0500 To: [email protected] From: [email protected] Subject: [sakai] svn commit: r39772 - content/branches/ Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=

Reading Files in Python

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 the for statement to iterate through a sequence
  • Remember - a sequence is an ordered set xfile = open('mbox.txt') for cheese in xfile: print(cheese)

Reading the Whole File

We can read the whole file (newlines and all) into a single string

> >> fhand = open('mbox-short.txt')

>>> inp = fhand.read()

>>> print(len(inp))

>>> print(inp[:20])

From stephen.marquar

Searching Through a File

We can put an if statement in our for loop to only print lines that meet some criteria

fhand = open('mbox-short.txt')

for line in fhand:

if line.startswith('From:') :

print(line)

  • Each line from the file has a newline at the end
  • The print statement adds a newline to each line What are all these blank lines doing here?

From: [email protected]\n

\n

From: [email protected]\n

\n

From: [email protected]\n

\n

From: [email protected]\n

\n

OOPS!

Searching Through a File (fixed)

  • We can strip the whitespace from the right-hand side of the string using rstrip() from the string library
  • The newline is considered “white space” and is stripped fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if line.startswith('From:') : print(line)

From: [email protected]

From: [email protected]

From: [email protected]

From: [email protected]

Using in to Select Lines

We can look for a string anywhere in a line as our selection criteria fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if not '@uct.ac.za' in line : continue print(line) From [email protected] Sat Jan 5 09:14:16 2008 X-Authentication-Warning: set sender to [email protected] using – f From: [email protected] Author: [email protected] From [email protected] Fri Jan 4 07:02:32 2008 X-Authentication-Warning: set sender to [email protected] using - f...

Prompt for

File Name

fname = input('Enter the file name: ') fhand = open(fname) count = 0 for line in fhand: if line.startswith('Subject:') : count = count + 1 print('There were', count, 'subject lines in', fname)

Enter the file name: mbox.txt

There were 1797 subject lines in mbox.txt

Enter the file name: mbox-short.txt

There were 27 subject lines in mbox-short.txt