Wed Aug 11 2021

Directory

File Name: directory-operation.py

#!/usr/bin/evn python 

import os
directory = input("Enter your directory name: ")
if not os.path.exists(directory):
    # Create directory
    os.makedirs(directory)
    print(directory," - directory successfully created!")
else:
    print("Directory ",directory," already exists!")
    
print("List all directory under /usr")
# List all directory
print(os.listdir("/usr"))

# Delete directory
os.rmdir(directory)
print(directory," - directory deleted successfully!")
    


#***** Output *****
# Enter your directory name: abcd
# abcd  - directory successfully created!
# List all directory under /usr
# ['include', 'local', 'bin', 'lib', 'src', 'sbin', 'lib64', 'share', 'lib32', 'etc']
# abcd  - directory deleted successfully!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.