Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Reading Files 7:00
- Basics 4 questions
- The Basics 5 objectives
- Escape Hatches 5:23
- Escapes 4 questions
- Escapes 2 objectives
- Counts 6:05
- Phone Numbers 1 objective
- Word Length 1 objective
- Sets 5:42
- Email 1 objective
- Negation 8:20
- Negated Numbers 1 objective
- Groups 9:45
- Name Groups 1 objective
- Email Groups 2 objectives
- Compiling and Loops 6:51
- Players Dictionary and Class 2 objectives
- Review: Regular Expressions in Python 8 questions

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Before we can search through our text, we have to be able to open the file it's contained in. Then we can start with some super-specific searches of our text.
Documentation
New Terms
-
open()
- Opens a file in Python. This won't contain the content of the file, it just points to it in memory. -
.read()
- Reads the entire contents of the file object it's called on. -
.close()
- Closes the file object it's called on. This clears the file out of Python's memory. -
r'string'
- A raw string that makes writing regular expressions easier. -
re.match(pattern, text, flags)
- Tries to match a pattern against the beginning of the text. -
re.search(pattern, text, flags)
- Tries to match a pattern anywhere in the text. Returns the first match.
A better way to read files
If you don't know the size of a file, it's better to read it a chunk at a time and close it automatically. The following snippet does that:
with open("some_file.txt") as open_file:
data = open_file.read()
The with
causes the file to automatically close once the action inside of it finishes. And the action inside, the .read()
, will finish when there are no more bytes to read from the file.
Why didn't I cover this in the video? There's more going on here, behind-the-scenes, and I think it's better to know the long way first.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Stheven Cabral
Full Stack JavaScript Techdegree Graduate 29,854 Points1 Answer
-
Andrei Oprescu
9,547 Points1 Answer
-
Jerimiah Willhite
12,014 Points1 Answer
-
Sahar Nasiri
7,454 Points1 Answer
-
MoatazBellah Ghobashy
9,518 Points1 Answer
-
vikas pal
11,563 Points2 Answers
-
PLUS
Hara Gopal K
Courses Plus Student 10,027 Points2 Answers
-
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points2 Answers
-
sidni ahmed
3,329 Points4 Answers
-
James N
17,864 Pointsis "regex" built-in to python? or do i have to download it seperatly?
Posted by James NJames N
17,864 Points1 Answer
-
Rajesh Tupakula
1,927 Points1 Answer
-
Graham Mackenzie
2,747 Points"This saves us some very confusing repeated uses of the backslash character or the escape character"
2 Answers
-
peter keves
6,854 Points1 Answer
-
Naivedya Bansal
5,483 Points1 Answer
-
PLUS
Babak Damadi
Courses Plus Student 3,133 Points2 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up