You are on page 1of 1

Microsoft: DEV274x

Introduction to Python: Fundamentals Help zahideme 

Course Discussion Wiki Progress

Course  Module 4 Files  File .readlines() and .close() methods  .readlines() method

 Previous      Next 

Audit Access Expires Apr. 14, 2019


You lose all access to this course, including your progress, on Apr. 14, 2019.
Upgrade by Mar. 31, 2019 to get:
Unlimited access as long as the course exists
Graded problems and exams
Certi cate of completion
Upgrade now

.readlines() method
 Bookmark this page

.readlines() method
Start of transcript. Skip to the end.

MSXIPFXX2017-V003900
Watch later Share

To open text les as a list, we can use the


.readlines() method.
In this example,
we've already imported our poem into our
Jupyter Notebook.
And we're going to open it and assign that open
to the poem1 variable.
So this is a read format open, and
we're going to use the .readlines() method on the
instance of the le.
and .readlines() will read in each line of
 0:02 / 1:53  Speed 1.0x  HD     the text le as a list item.

Video Transcripts
Download video le Download SubRip (.srt) le
Download Text (.txt) le

Concept

.readlines()

File read as a list with .readlines()


converts the lines of a le into a list of strings

poem_lines = poem1.readlines()

Examples
# [ ] Run to download file to notebook
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt

# [ ] review and run example


# open address to file
poem1 = open('poem1.txt', 'r')

# readlines and print as a list


poem_lines = poem1.readlines()
poem_lines

# [ ] review and run example


for line in poem_lines:
print(line)

Task 1
.readlines()

open the cities le as a list

1. Import a list of cities using curl

1. git the list from https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities

2. name the list cities.txt

2. Open cities.txt in read mode using a variable: cities_ le

3. Read cities_ le as a list variable: cities_lines using .readlines()

4. Print each line of cities_lines by iterating the list

# [ ] import cities

# [ ] open cities.txt as cities_file and read the file as a list: cities_lines

# [ ] use list iteration to print each city in cities_lines list

 Previous Next 

Learn About Veri ed Certi cates


© All Rights Reserved

edX Legal Connect


     
About Terms of Service & Blog
edX for Business Honor Code Contact Us
Privacy Policy Help Center
Accessibility Policy

© 2012–2019 edX Inc.


EdX, Open edX, and MicroMasters are
registered trademarks of edX Inc. | 粤ICP备
17044299号-2

You might also like