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:
import os
# 1) Prompt the user for a directory name
dir_name = input()
# 2) Create the directory
os.mkdir(dir_name)
# 3) Verify the directory was created by listing the content of the current working directory
print('Current directory content:', os.listdir())
# 4) Remove the created directory
os.rmdir(dir_name)
# 5) Verify the directory was removed by listing the content of the current working directory
print('Current directory content:', os.listdir())
# --Completed--
# [ ] Write a program to:
import os
# 1) Create a directory called "my_dir"
os.mkdir('my_dir')
# 2) Change the current working directory to "my_dir"
os.chdir('my_dir')
# 3) Verify you are in the correct directory by displaying the current working directory
print('Current working directory:', os.getcwd())
# 4) Change the working directory back to the parent directory
os.chdir('..')
# 5) Verify you are in the correct directory by displaying the current working directory
print('Current working directory:', os.getcwd())
# 6) Rename "my_dir" to "your_dir"
os.rename('my_dir', 'your_dir')
# 7) Verify the directory was renamed by listing the content of the current working directory
print(os.listdir(os.getcwd()))
# 8) Remove "your_dir"
os.rmdir('your_dir')
# 9) Verify the directory was removed by listing the content of the current working directory
print(os.listdir(os.getcwd()))
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
import os, os.path
name = input('Enter a file or directory name.')
current = os.getcwd()
# then prints a message verifying if it exists in the current working directory
if os.path.exists(name):
if name in current:
print('This file or directory is in the current working directory.')
else:
print('File or directory does not exist. These are the directorys that do exist:', os.listdir())
# [ ] Write a program to print the absolute path of all directories in "parent_dir"
import os, os.path
# HINTS:
# 1) Verify you are inside "parent_dir" using os.getcwd()
os.chdir('/work/Mod 1')
# 2) Use os.listdir() to get a list of files and directories in "parent_dir"
liist = os.listdir(os.getcwd())
# 3) Iterate over the elements of the list and print the absolute paths of all the directories
for item in liist:
print(os.path.abspath(item))