#Madelyn Parker
#November 4, 2021
#U3M1A1.4
#File System
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=input("please enter the name you would like to give a directory: ")
os.mkdir(dir)
print("current directory content: " , os.listdir(), "\n")
os.rmdir(dir)
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("current directory is" , os.getcwd(), "\n")
os.chdir('..')
print("updated current working directory is ", os.getcwd(),"\n")
os.rename("my_dir","your_dir")
print("updated current workng directory with renamed directory", os.listdir(), "\n")
os.rmdir("your_dir")
print("updated current woking directory woth your_dir removed",os.listdir())
import os, os.path
# Print the current working directory (should be "parent_dir")
print('The current working directory is:', os.getcwd())
# 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
import os.path
name = input("please enter a file or directory name: ")
if (os.path.exists(name)):
print("the file or directory exists in th current working directory")
else:
print("the file or directory does not 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, os.path
print("the current working directory is: ",os.getcwd())
listOffFilesDir=os.listdir()
for item in listOffFilesDir:
if(os.path.exists(item)):
print(os.path.abspath(item))