# Noemi Cabrera
# 14 November 2021
# In this lesson, I learned how to identify the platform running a Python script ('Linux', 'win32', 'Darwin') using sys, how to get
# the current working directory using os.getcwd(), how to change the current working directory using oschdir(), how to list the content
# of the current working directory using os.listdir(), how to create a new directory using os.mkdir(), how to remove a directory using
# os.rmdir(), how to rename files and directories using os.rename(). Additionally, I learned to recognize the difference between relative
# and absolute paths, as well as how to test whether a path exists using os.path.exists. Lastly, to test whether a specific file or directory
# exists, I used os.path.isfile and os.path.isdir.
# I had some difficulties trying to figure out why this notebook said some files/directories didn't exist even though the code for the
# examples that I have to run were supposed to be correct. Since this was a problem with the code already provided to me, I do not know
# how to fix it.
# ************** 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)
# IN this code, the words 'sys' and 'platform' are used to identify what operating system is
# being used. In this case, the operating system is linux.await
import sys
print(sys.platform)
# In this code, the os module is used. The current working directory is changed to parent_dir,
# then to child1_dir, then back to parent_dir by using r using os.chdir(). There is a printout
# of the current working directory at every step, which was done by using os.getcwd() in the
# print statement.
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())
# In this code the os module is used. There is a printout of the current working directory,
# which was done by using os.getcwd() in the print statement. The contents of the directory
# are accessed by using os.listdir().
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())
# In this code the os module is used. There is a printout of the current working directory,
# which was done by using os.getcwd() in the print statement. Then, a new directory named
# "new child" is created by using os.mkdir(). This directory is then removed from the current
# directory by using os.rmdir().
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())
# In this code the os module is used. There is a printout of the current working directory,
# which was done by using os.getcwd() in the print statement. Then, a new directory named
# "new child" is created by using os.mkdir(). This new directory is renamed to "old child" using
# os.rename(). This directory is then removed from the current directory by using os.rmdir().
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())
# # In this code the os module is used. The user is asked to enter a file name. Then a
# directory is created with the name the user entered. There is a printout displaying the
# contents of the current working directory, which was done by using os.listdir() in the
# print statement. This directory is then removed from the current directory by using
# os.rmdir(). Lastly, there is a printout displaying the contents of the current working 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
dir_name = input("Enter a directory(file) name:")
os.mkdir(dir_name)
print('Current directory content: ', os.listdir())
os.rmdir(dir_name)
print('Current directory content: ', os.listdir())
# In this code, the os module is used. A directory named "my_dir" is created using os.mkdir()
# and then the location is moved to this new file. There is a printout of the current working
# directory, which was done by using os.getcwd() in the print statement. Then, the location is
# moved to the parent directory using os.chdir(). The "my_dir" directory is renamed to "your_dir"
# using os.rename(). This directory is then removed from the current directory by using os.rmdir().
# [ ] 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())
# In this code, the os module is used. There is a printout of the current working
# directory, which was done by using os.getcwd() in the print statement, and a printout of
# the path to get to the current directory.
# Then, using the os.path.abspath() method, the absolute path to child1_dir/leaf1.txt is
# stored ito the abs_path variable and displayed. The, the path is tested to see if it exists
# using the os.path.exists() method. If it exists, then the type (file directory) of it it's displayed.
# If it doesn't exist, then a printout sayig that is displayed.
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")
# In this code, the os module is used. The user asked to enter a file name, which is stored in
# the 'file_name' variable. Then a printout of the current working directory is displayed by using
# os.getcwd(). Lastly, if the file name given by the user is found exists in the current working
# directory, then a statement saying so will display. If it doesn't exist in the current working
# directory, then a statement saying so will display.
# [ ] 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
file_name = input("Enter a file name: ")
print('The current working directory 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")
# In this code, the os module is used. A printout of the current working directory is displayed
# by using os.getcwd(). Then, the contents of the this directory is dislpayed by using o.listdir().
# Lastly, a for..in loop interates through each item in the contents and displays the absolute path
# for each item.
# [ ] 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))