# ************** 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')
file_path = "./parent_leaf.txt"
f = open(file_path, 'w')
f.close()
file_path = "./text_file.txt"
f = open(file_path, 'w')
f.close()
os.mkdir('child1_dir')
os.chdir('child1_dir')
file_path = "./leaf1.txt"
f = open(file_path, 'w')
f.close()
os.chdir('../')
os.mkdir('child2_dir')
os.chdir('child2_dir')
file_path = "./leaf2.txt"
f = open(file_path, 'w')
f.close()
os.chdir('../..')
current_path = os.getcwd()
print("Current working directory:", current_path)
import sys
print(sys.platform)
import os
# Change the current working directory to parent dir
os.chdir('parent_dir')
print('Changed working dir to parent: ', os.getcwd())
# Change the current working directory to child1 dir
os.chdir('child1_dir')
print('Changed working dir to child1: ', os.getcwd())
# Change the current working directory back to the parent dir
os.chdir('..')
print('Changed working dir back to parent: ', os.getcwd())
import os
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# List the content of the directory (both files and other directories)
print('Current directory content: ', os.listdir())
import os
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# Create a new directory
os.mkdir('new_child')
print('Created new_child dir')
# List the content of the directory
print('Current directory content: ', os.listdir())
# Remove the new child directory
os.rmdir('new_child')
print('Removed new_child dir')
# List the content of the directory
print('Current directory content: ', os.listdir())
import os
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# Create a new directory
os.mkdir('new_child')
print('Created new_child dir')
# List the content of the directory
print('Current directory content:', os.listdir())
# Rename new_child as old_child
os.rename('new_child', 'old_child')
print('Renamed new_child as old_child')
# List the content of the dir
print('Current directory content: ', os.listdir())
# Remove the old_child dir
os.rmdir('old_child')
print('Removed old_child dir')
print('Current directory content: ', os.listdir())
# [ ] Write a program to:
# 1) Prompt the user for a directory name
# 2) Create the directory
# 3) Verify the directory was created by listing the content of the current working directory
# 4) Remove the created directory
# 5) Verify the directory was removed by listing the content of the current working directory
import os
dir_name = input("Input a directory(file) name:")
os.mkdir(dir_name)
print('Current directory content: ', os.listdir())
os.rmdir(dir_name)
print('Current directory content: ', os.listdir())
# [ ] Write a program to:
# 1) Create a directory called "my_dir"
# 2) Change the current working directory to "my_dir"
# 3) Verify you are in the correct directory by displaying the current working directory
# 4) Change the working directory back to the parent directory
# 5) Verify you are in the correct directory by displaying the current working directory
# 6) Rename "my_dir" to "your_dir"
# 7) Verify the directory was renamed by listing the content of the current working directory
# 8) Remove "your_dir"
# 9) Verify the directory was removed by listing the content of the current working directory
import os
os.mkdir("my_dir")
os.chdir("my_dir")
print('The current working directory is:', os.getcwd())
os.chdir("/work")
print('The current working directory is:', os.getcwd())
os.rename("my_dir", "your_dir")
print('Current directory content: ', os.listdir())
os.rmdir("your_dir")
print('Current directory content: ', os.listdir())
import os, os.path
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
print(os.path)
# Find the absolute path to child1_dir/leaf1.txt
abs_path = os.path.abspath("child1_dir/leaf1.txt")
print("Absolute path to leaf1.txt is: ", abs_path)
# Test whether the path exists
if(os.path.exists(abs_path)):
print("Path exists")
# Test to see if it's a file or directory
if(os.path.isfile(abs_path)):
print("It's a file")
elif (os.path.isdir(abs_path)):
print("It's a dir")
else:
print("Path doesn't exist")
# [ ] Write a program that prompts the user for a file or directory name
# then prints a message verifying if it exists in the current working directory
file_name = input("Enter a file name: ")
print('The current directory in progress is:', os.getcwd())
print(os.path)
print()
if(os.path.exists(file_name)):
print(file_name, "exists in the current working directory")
else:
print(file_name, "- doesn't exist in the current working directory")
# [ ] Write a program to print the absolute path of all directories in "parent_dir"
# HINTS:
# 1) Verify you are inside "parent_dir" using os.getcwd()
# 2) Use os.listdir() to get a list of files and directories in "parent_dir"
# 3) Iterate over the elements of the list and print the absolute paths of all the directories
import os
print('The current working directory is:', os.getcwd())
print(os.listdir("parent_dir"))
for element in parent_dir:
if(os.path.exists(item)):
print(os.path.abspath(item))