# %cd command_line
# Note:
#Above was the original code given by Microsoft.
# This will work the first time, but if you re-run these cells
# you may encounter errors so we changed it to...
# ************** DO NOT MODIFY OR ADD ANYTHING TO THIS CODE SEGMENT ***********
# ************** Click Edit> Clear All Outputs ***********
# ************** This code segment must be run before attempting any of the tasks or examples in this lesson ********************
# ************** This cell is a reset cell to remove (if they exist) the parent_dir and any children or leaves
# It prepares the directories and files necessary to run the examples and complete the tasks.
# This cell can be run first then all others cells in this notebook can be run, then this cell can be rerun
# This is the reset. It uses the import shutil (or shell utility) which you will learn later in the course
import os, shutil # shutil = shell utility has an important method called rmtree()
# linux does not have an equivalent command!
# Navigate to `library` directory (if not already in it)
current_path = os.getcwd()
print(current_path)
if ("content" in current_path):
#JGif ("library" in current_path):
nb_path = current_path.split("content")[0] # nb_path = the 1st item in the split list
else:
nb_path = current_path
print("Changing working dir to directory above parent_dir")
os.chdir(os.path.join(nb_path,"content"))
current_path = os.getcwd()
print("Current working directory:", current_path)
if (os.path.exists('parent_dir') == True):
shutil.rmtree('parent_dir')
os.mkdir('parent_dir')
os.chdir('parent_dir')
os.mkdir('child1_dir')
os.chdir('../')
current_path = os.getcwd()
print("Current working directory:", current_path)#
%mkdir command_line
%cd command_line
!pwd
/work
Changing working dir to directory above parent_dir
Execution Error
NameError: name 'content' is not defined
%%writefile c2f.py
def C2F(degrees_celsius):
""" Convert Celsius to Fahrenheit"""
return degrees_celsius * (9/5) + 32
print("Accessing docstrings using __doc__:\n")
print(C2F.__doc__)
Writing c2f.py
%%bash
pydoc c2f
Accessing docstrings using __doc__:
Convert Celsius to Fahrenheit
Help on module c2f:
NAME
c2f
FUNCTIONS
C2F(degrees_celsius)
Convert Celsius to Fahrenheit
FILE
/work/c2f.py
%%writefile kg2lb.py
def kg2lb(kilograms):
"""
Convert kilograms to pounds
args:
kilograms: float weight in kg
returns:
pounds: float weight in lb
"""
pounds = kilograms * 2.20462262185
return pounds
# the following is needed to make sure that none of the code in the function is run
if __name__ == "__main__":
pass
Writing kg2lb.py
%%bash
pydoc kg2lb
Help on module kg2lb:
NAME
kg2lb
FUNCTIONS
kg2lb(kilograms)
Convert kilograms to pounds
args:
kilograms: float weight in kg
returns:
pounds: float weight in lb
FILE
/work/kg2lb.py
%%writefile currency_converter.py
def USD2EUR(amount):
"""
Convert amount from US Dollars to Euros.
Use 1 USD = 0.831467 EUR
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Euros (float)
"""
return value
def EUR2GBP(amount):
"""
Convert amount from Euros to British Pounds.
Use 1 EUR = 0.889358 GBP
args:
amount: Euros amount (float)
returns:
value: the equivalent of amount in GBP (float)
"""
return value
def USD2GBP(amount):
"""
Convert amount from US Dollars to British Pounds.
The conversion rate is unknown, you have to use USD2EUR and EUR2GBP
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in British Pounds (float)
"""
return value
if __name__ == "__main__":
pass
Overwriting currency_converter.py
%%bash
pydoc currency_converter
Help on module currency_converter:
NAME
currency_converter
FUNCTIONS
EUR2GBP(amount)
Convert amount from Euros to British Pounds.
Use 1 EUR = 0.889358 GBP
args:
amount: Euros amount (float)
returns:
value: the equivalent of amount in GBP (float)
USD2EUR(amount)
Convert amount from US Dollars to Euros.
Use 1 USD = 0.831467 EUR
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Euros (float)
USD2GBP(amount)
Convert amount from US Dollars to British Pounds.
The conversion rate is unknown, you have to use USD2EUR and EUR2GBP
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in British Pounds (float)
FILE
/work/currency_converter.py