2-4.1 Intro Python

Working with Files

4.1 File import in Jupyter Notebooks
4.1 File open() and .read()
4.2 File Read as a list with .readlines()
4.2 File Closing to free resources with .close()
4.3 File Read a line at a time with .readline()
4.3 Remove characters using .strip()
4.4 File .write() with .seek()
4.4 File append mode


> Student will be able to
4.1 Import files in Jupyter Notebooks using the curl command
4.1 open() and .read() local files in memory
4.1 .read() a specific number of characters
4.2 Use .readlines() to read data from file as a list of lines
4.2 Use .close to free system resources
4.3 Use .readline() to read data from file a line at a time
4.3 Use .strip() to remove new line characters
4.4 .write() data to a new local file 4.4 Use .seek() to set file read or write location
4.4 Use file append mode

 

Concepts

Import Files to Jupyter

view video

curl imports files to Jupyter session from a web address

below is a code using curl to import poem1.txt, the code is in a command line interface syntax

>#### !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt

The table explains each element of the command above

code meaning
! runs command interface supporting curl
curl enables curl that can download files
https://raw.githubusercontent.com/... is the address for data file to import
-o tells curl write data to a file
poem1.txt name curl will give the file

 

Examples

In [1]:
# [ ] review and run example
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0     32      0  0:00:01  0:00:01 --:--:--    33

 

Concepts

Opening a Local File in read mode

>python poem_file = open('poem1.txt', 'r')
view video

Read mode   'r'

MODE Description
'r' read only mode
'w' write - overwrites file with same name
'r+' read and write mode
'a' opens for appending to end of file

open() creates an object that can be addressed in python code

 

Examples

In [2]:
# [ ]Run to open the file in memory as poem_file
poem_file = open('poem1.txt', 'r')
In [3]:
# [ ] run and review code to test if open worked 
# should display name='poem1.txt' and no errors
poem_file
Out[3]:
<_io.TextIOWrapper name='poem1.txt' mode='r' encoding='UTF-8'>

 

Task 1

import and open a local file in read mode

  1. Import a list of cities using curl
    a. git the list from https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities
    b. name the list cities.txt
  2. open cities.txt in read mode using a variable = cities_file
  3. test that cities_file opened cities.txt with a print statement
In [26]:
# [ ] import cities.txt

#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0    284      0 --:--:-- --:--:-- --:--:--   287
In [27]:
# [ ] open cities.txt as cities_file
# [ ] test cities.txt was opened 
#Answer:
cities_file = open('cities.txt', 'r')
cities_file
Out[27]:
<_io.TextIOWrapper name='cities.txt' mode='r' encoding='UTF-8'>

 

Concepts

Read a file using .read()

view video

reading text

poem_contents = poem_file.read()

.read() loads the content of the file into memory as a string, including formatting such as new line (\n)

 

Examples

examples expect that the cells that import and open of poem1.txt has been run without a read()

Run the cells that import and open poem1.txt

In [6]:
# [ ] review and run example
poem_contents = poem_file.read()
In [7]:
# [ ] review and run example
# shows the file as a string with formatting characters such as "\n", output should be non-blank
poem_contents
Out[7]:
'Loops I repeat\nloops\nloops\nloops\nI repeat\nuntil I\nbreak\n'
In [8]:
# [ ] review and run example
# since .read() loaded the file as a string it can be printed
print(poem_contents)
Loops I repeat
loops
loops
loops
I repeat
until I
break

 

Task 2

read a file

Read the file cities.text that was imported in task 1

  1. import cities.txt and open
    a. ensure the code was created and run in task 1 to import cities.txt
    b. create and run code to re-open cities.txt as cities_file
  2. read() cities_file into a variable called cities
  3. Test the read() by displaying the string contained in cities
  4. Test the read() by printing the cities string
In [28]:
# [ ] after import and open of cities.txt in task 1
# [ ] read cities_file as cities
# [ ] display the string: cities

#Answer:
cities = cities_file.read()
In [29]:
# [ ] print the string: cities

#Answer:
print (cities)
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo

 

Concepts

reading a file with .read(n) where n = number of characters to read

view video

each time poem_file.read(10) runs, the next 10 characters are read.

> Note: if .read(10) result is = ''  (or empty string with no characters), it is likely that the end of the file has been reached. Perform a fresh .open() to reset read() to the beginning of the file.

 

Examples

examples expect that the cells that import and open of poem1.txt has been run without a read()

Run the cell at the top of the notebook to import poem1.txt
each line is a different approach to reading and displaying 10 characters of the poem

In [30]:
# [ ] review and run example to read poem1.txt 10 characters at a time
poem_file = open('poem1.txt', 'r')
poem_10char = poem_file.read(10)
print(poem_10char)
poem_10char
Loops I re
Out[30]:
'Loops I re'
In [31]:
# [ ] review and run example, + 10 more characters
# reads and displays without storing in a variable
poem_file.read(10)
Out[31]:
'peat\nloops'
In [32]:
# [ ] review and run example, + 10 more characters
# reads and stores in variable poem_parts
poem_parts = poem_file.read(10)
print(poem_parts)
loops
loo
In [33]:
# [ ] REPEATEDLY RUN this cell,  + 5 more characters each time run are appended using string addition
# [ ]  consider why no additional text displays after multiple runs
poem_parts += poem_file.read(5)
print(poem_parts)
loops
loops
I 

 

Task 3

digits of pi

read a set number of digits with .read(n)

import, open, read, print

  1. import digits_of_pi.txt located at https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi
  2. open as digits_of_pi_text
  3. read()the first 4 characters of digits_of_pi_text into a variable called pi_digits
  4. print pi_digits
  5. add to pi_digits string with string addition
    a. add next 4 characters from digits_of_pi obtained from read()
    b. run the cell multiple times to get more digits of pi
In [34]:
# [ ] digits of pi
# 1. import digits_of_pi.txt

#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi -o digits_of_pi.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   303  100   303    0     0    804      0 --:--:-- --:--:-- --:--:--   812
In [38]:
# [ ] digits of pi
# 2. open as digits_of_pi_text 
# 3. read() 4 char of digits_of_pi_text to pi_digits variable 
# 4. print pi_digits  

#Answer:
digits_of_pi_text = open('digits_of_pi.txt', 'r')
pi_digits = digits_of_pi_text.read(4)
print(pi_digits)
3.14
In [41]:
# [ ] digits of pi
# 5. add to pi_digits string with string addition  
#   a. add next 4 characters from digits_of_pi obtained from read()  
#   b. run the cell multiple times to get more digits of *pi*

#Answer:
pi_digits += digits_of_pi_text.read(4)
print(pi_digits)
3.14159265358979

 

Concept

.read() returns a string

These strings can be manipulated just like any other string

view video

Boolean tests such as:

  • .upper()
  • .title()
  • string slices, e.g.- cities[3:9]
  • etc..

and string methods can be performed such as:

  • .isdigit()
  • .isalpha()
  • etc...

 

Examples

examples expect that the cells that import has been run

it may be necessary to run the cell to import poem1.txt at top of notebook

In [42]:
# [ ] review and run example
poem_file = open('poem1.txt', 'r')
poem_part = poem_file.read(15).upper()
print(poem_part)
LOOPS I REPEAT

In [43]:
# [ ] review and run example
poem_part = poem_file.read(6).title()
print(poem_part)
Loops

In [44]:
# [ ] review and run example
poem_part = poem_file.read(6)
print(poem_part)
print(poem_part.isalpha(), "isalpha() because of `\\n`")
poem_part
loops

False isalpha() because of `\n`
Out[44]:
'loops\n'
In [45]:
# [ ] review and run example
poem_file = open('poem1.txt', 'r')
poem_text = poem_file.read()
print(poem_text[8:26])
repeat
loops
loops

 

Task 4

City Initials

Read the file cities.text that was imported in task 1

  1. ensure the code was created and run in task 1 to import cities.txt
  2. create and run code to re-open cities.txt as cities_file
  3. read() cities_file into a variable called cities
  4. iterate through the characters in cities a. test if .isupper(), if True append the character to a string variable: initials c. else if (elif) character is "\n", if True append the "\n" to initials
  5. print initials
In [46]:
# [ ] compelete the task

#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0     94      0 --:--:-- --:--:-- --:--:--    94
In [48]:
cities_file = open('cities.txt', 'r')
cities = cities_file.read()
initials = ""
for letter in cities:
    if (letter.isupper()):
        initials += letter
    elif (letter=="\n"):
        initials += "\n"
print('initials: ',initials)
initials:  B
C
L
N
NYC
S
T

Terms of use   Privacy & cookies   © 2017 Microsoft