# My name is Pavan Kumpatla and today's date November 23, 2021.
# I learned how to use .os and also learned paths and how to use them
# I also learned about directories
# I had a lot of difficulty since it kept saying error in some things
# so I couldn't verify if they worked or not
# NOTE:- I'm using deepnote to see if it works here
# NOTE:- I'm using a different platform right now called DeepNote
# NOTE:- I thought it might work here but it still says
# NOTE:- but this error is a different one.
# ************** 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
# imports
print(sys.platform)
# prints the platform
import os
# imports
# Change the current working directory to parent dir
os.chdir('parent_dir')
# changes directory
print('Changed working dir to parent: ', os.getcwd())
# prints, gets cwd (current working directory)
# Change the current working directory to child1 dir
os.chdir('child1_dir')
# changes directory
print('Changed working dir to child1: ', os.getcwd())
# prints, gets cwd
# Change the current working directory back to the parent dir
os.chdir('..')
# goes back 1 level
print('Changed working dir back to parent: ', os.getcwd())
# prints, gets cwd
import os
# imports
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# prints, gets cwd
# List the content of the directory (both files and other directories)
print('Current directory content: ', os.listdir())
# prints, lists everything in the directory
import os
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# prints, gets cwd
# Create a new directory
os.mkdir('new_child')
# creates directory
print('Created new_child dir')
# prints
# List the content of the directory
print('Current directory content: ', os.listdir())
# prints, lists everything in the directory
# Remove the new child directory
os.rmdir('new_child')
# removes it from directory
print('Removed new_child dir')
# prints
# List the content of the directory
print('Current directory content: ', os.listdir())
# prints, lists everything in the directory
import os
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# prints, gets cwd
# Create a new directory
os.mkdir('new_child')
# creates new directory
print('Created new_child dir')
# prints
# List the content of the directory
print('Current directory content:', os.listdir())
# prints, lists everything in the directory
# Rename new_child as old_child
os.rename('new_child', 'old_child')
# renames new_child to old_child
print('Renamed new_child as old_child')
# prints
# List the content of the dir
print('Current directory content: ', os.listdir())
# prints, lists everything in the directory
# Remove the old_child dir
os.rmdir('old_child')
# removes old_child
print('Removed old_child dir')
# prints
print('Current directory content: ', os.listdir())
# prints, lists everything in the directory
# [ ] 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
# imports
directory_name = input("Type in a name for a directory:- ")
# gathers user input and stores it
os.mkdir(directory_name)
# makes new directory
print("Adding Name...")
# prints
print("Current directory content: ", os.listdir())
# prints, lists everything in the directory
os.rmdir(directory_name)
# removes directory
print("Deleting Name...")
# prints
print("Current directory content: ", os.listdir())
# prints, lists everything in the directory
# [ ] 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
os.mkdir("my_dir")
# makes new directory
os.chdir("my_dir")
# changes cwd
print("Current directory content: ", os.getcwd())
# prints
os.chdir("..")
# changes back - I think this is right? Please do tell if it is wrong
print("Current directory content: ", os.getcwd())
# prints
os.rename("my_dir", "your_dir")
# renames
print("Current directory content: ", os.listdir())
# prints
os.rmdir("your_dir")
# removes directory
print("Current directory content: ", os.listdir())
# prints
import os, os.path
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# prints, prints cwd
print(os.path)
# prints path
# Find the absolute path to child1_dir/leaf1.txt
abs_path = os.path.abspath("child1_dir/leaf1.txt")
# finds absolute path, stores it
print("Absolute path to leaf1.txt is: ", abs_path)
# prints
# Test whether the path exists
if(os.path.exists(abs_path)):
# if abs_path exists
print("Path exists")
# prints
# Test to see if it's a file or directory
if(os.path.isfile(abs_path)):
# if abs_file is a file
print("It's a file")
# prints
elif (os.path.isdir(abs_path)):
# else if abs_path is a directory
print("It's a dir")
# prints
else:
# else
print("Path doesn't exist")
# prints
# [ ] 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
import os
# imports
print('The current working directory is:', os.getcwd())
# prints, prints cwd
user = input("Type in a name for a file or directory:- ")
# gathers user input
if os.path.exists(user):
# if user exists
print(user, "does exist")
# prints
else:
# else
print(user, "does not exist")
# prints
# [ ] 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
# imports
print("The current working directory is:", os.getcwd())
# prints, prints cwd
list1 = os.listdir("parent_dir")
# gets a list of files and directories in "parent_dir" and stores
# I think this is how you do it?
for element in list1:
# for element in list1
if os.path.exists(element):
# if element exists
print(os.path.abspath(element))
# prints absolute path
# NOTE:- I'm not sure if I did this right.