Skip to content

Instantly share code, notes, and snippets.

@domm-hub
Created October 5, 2023 11:09
Show Gist options
  • Save domm-hub/609a5bc70d4eebefbf60edf2112cc2d3 to your computer and use it in GitHub Desktop.
Save domm-hub/609a5bc70d4eebefbf60edf2112cc2d3 to your computer and use it in GitHub Desktop.
import os
while True:
print("\n1. Make File or Add info to existing file")
print("2. Delete file or folder")
print("3. Make Folder")
print("4. Make .txt file")
print("5. View all files and folders in the current directory")
prominput = input("What do you want to do? ")
if prominput == "1":
try:
path = input("Enter the file path: ")
content = input("Enter the content of the file: ")
with open(path, "w") as file:
file.write(content)
print("File created successfully.")
except Exception as e:
print("An error occurred:", str(e))
elif prominput == "2":
try:
path = input("Enter the file or folder path: ")
if os.path.isfile(path):
os.unlink(path)
print("File deleted successfully.")
elif os.path.isdir(path):
os.rmdir(path)
print("Folder deleted successfully.")
else:
print("Path does not exist.")
except Exception as e:
print("An error occurred:", str(e))
elif prominput == "3":
try:
path = input("Enter the new folder's path: ")
os.makedirs(path)
print("Folder created.")
except Exception as e:
print("An error occurred:", str(e))
elif prominput == "4":
try:
path = input("Enter the .txt file path: ")
content = input("Write the content of the file:")
with open(path + ".txt", "w") as file:
file.write(content)
print("Text file created")
except Exception as e:
print("An error occurred:", str(e))
elif prominput == "5":
try:
current_directory = os.getcwd()
items = os.listdir(current_directory)
print("Items in Current Directory:")
for item in items:
item_path = os.path.join(current_directory, item)
if os.path.isfile(item_path):
print(f"File: {item} (Type: File)")
elif os.path.isdir(item_path):
print(f"Directory: {item} (Type: Directory)")
else:
print(f"Unknown: {item} (Type: Unknown)")
except Exception as e:
print("An error occurred: ", str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment