Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import platform
- import subprocess
- import time
- def clear_screen():
- """Clears the command prompt screen based on the operating system."""
- if platform.system() == "Windows":
- os.system('cls')
- else:
- os.system('clear')
- def close_explorer():
- if platform.system() == "Windows":
- try:
- subprocess.run("taskkill /f /im explorer.exe", shell=True)
- time.sleep(1)
- except Exception:
- pass
- clear_screen()
- print("Yo! This is a small project I work on when I'm bored.")
- print("Exit using the [[exit]] command to recover explorer.exe")
- def open_explorer():
- if platform.system() == "Windows":
- try:
- subprocess.run("start explorer.exe", shell=True)
- except Exception:
- pass
- def print_help():
- """Prints a list of all available commands and how to use them."""
- help_text = """
- Available commands:
- create [[NAME]] [[FOLDER OR FILE]] - Create a folder or file with the specified name.
- rename [[OLD NAME]] [[NEW NAME]] - Rename a file or folder.
- changepath [[PATH]] - Change the current working directory to the specified path.
- delete [[FILE NAME]] - Delete the specified file.
- edit [[FILE]] [[MODE]] - Edit a file in one of three modes: overwrite, add, or rewrite.
- read [[FILE]] - Read and display the contents of the specified file.
- filecheck - List all files in the current directory with their sizes.
- clear - Clear the screen.
- exit - Exit the program.
- help - Display this help message.
- Edit modes for the 'edit' command:
- overwrite - Completely replace the file's content.
- add - Append new content to the end of the file.
- rewrite - Modify specific lines within the file.
- Examples:
- create my_folder folder
- create my_file.txt file
- rename old_name.txt new_name.txt
- changepath C:\\Users\\NewFolder
- delete my_file.txt
- edit my_file.txt overwrite
- read my_file.txt
- filecheck
- clear
- """
- print(help_text)
- def main():
- close_explorer()
- base_directory = os.getcwd()
- print(f"Current working directory: {base_directory}")
- while True:
- command = input("\nEnter a command: ").strip()
- if command.lower() == "exit":
- print("Exiting the program. Goodbye!")
- break
- elif command.lower() == "help":
- print_help()
- elif command.lower().startswith("create"):
- parts = command.split()
- if len(parts) == 3:
- name = parts[1]
- item_type = parts[2].lower()
- if item_type == "folder":
- try:
- os.makedirs(os.path.join(base_directory, name))
- print(f"Folder '{name}' created successfully.")
- except FileExistsError:
- print(f"Folder '{name}' already exists.")
- except Exception as e:
- print(f"Error creating folder '{name}': {e}")
- elif item_type == "file":
- try:
- file_path = os.path.join(base_directory, name)
- if not os.path.exists(file_path):
- with open(file_path, 'w') as file:
- file.write('')
- print(f"File '{name}' created successfully.")
- else:
- print(f"File '{name}' already exists.")
- except Exception as e:
- print(f"Error creating file '{name}': {e}")
- else:
- print("Invalid type. Please specify 'folder' or 'file'.")
- else:
- print("Invalid command format. Use: create [[NAME]] [[FOLDER OR FILE]]")
- elif command.lower().startswith("rename"):
- parts = command.split(maxsplit=2)
- if len(parts) == 3:
- old_name = parts[1]
- new_name = parts[2]
- old_path = os.path.join(base_directory, old_name)
- new_path = os.path.join(base_directory, new_name)
- if os.path.exists(old_path):
- try:
- os.rename(old_path, new_path)
- print(f"'{old_name}' has been renamed to '{new_name}'.")
- except Exception as e:
- print(f"Error renaming '{old_name}': {e}")
- else:
- print(f"The file or folder '{old_name}' does not exist.")
- else:
- print("Invalid command format. Use: rename [[OLD NAME]] [[NEW NAME]]")
- elif command.lower().startswith("changepath"):
- parts = command.split(maxsplit=1)
- if len(parts) == 2:
- new_path = parts[1]
- if os.path.isdir(new_path):
- base_directory = new_path
- print(f"Path changed to: {base_directory}")
- else:
- print(f"The path '{new_path}' does not exist or is not a directory.")
- else:
- print("Invalid command format. Use: changepath [[PATH]]")
- elif command.lower().startswith("delete"):
- parts = command.split(maxsplit=1)
- if len(parts) == 2:
- file_name = parts[1]
- file_path = os.path.join(base_directory, file_name)
- if os.path.isfile(file_path):
- try:
- os.remove(file_path)
- print(f"File '{file_name}' has been deleted.")
- except Exception as e:
- print(f"Error deleting file '{file_name}': {e}")
- else:
- print(f"The file '{file_name}' does not exist.")
- else:
- print("Invalid command format. Use: delete [[FILE NAME]]")
- elif command.lower().startswith("edit"):
- parts = command.split(maxsplit=2)
- if len(parts) == 3:
- file_name = parts[1]
- mode = parts[2].lower()
- file_path = os.path.join(base_directory, file_name)
- if not os.path.isfile(file_path):
- print(f"The file '{file_name}' does not exist.")
- continue
- if mode == "overwrite":
- try:
- print("Enter the new content. Type 'EOF' on a new line to finish.")
- with open(file_path, 'w') as file:
- while True:
- line = input()
- if line == "EOF":
- break
- file.write(line + '\n')
- print(f"File '{file_name}' has been overwritten successfully.")
- except Exception as e:
- print(f"Error overwriting file '{file_name}': {e}")
- elif mode == "add":
- try:
- print("Enter the content to add. Type 'EOF' on a new line to finish.")
- with open(file_path, 'a') as file:
- while True:
- line = input()
- if line == "EOF":
- break
- file.write(line + '\n')
- print(f"Content has been added to '{file_name}' successfully.")
- except Exception as e:
- print(f"Error adding content to file '{file_name}': {e}")
- elif mode == "rewrite":
- try:
- with open(file_path, 'r') as file:
- lines = file.readlines()
- print("Current content of the file:")
- for i, line in enumerate(lines, start=1):
- print(f"{i}: {line.strip()}")
- print("\nEnter new content for each line. Leave empty to keep the current line. Type 'EOF' when done.")
- with open(file_path, 'w') as file:
- for i, line in enumerate(lines, start=1):
- new_line = input(f"Line {i}: ")
- if new_line == "EOF":
- file.write(line)
- break
- elif new_line.strip() == "":
- file.write(line)
- else:
- file.write(new_line + '\n')
- print(f"File '{file_name}' has been rewritten successfully.")
- except Exception as e:
- print(f"Error rewriting file '{file_name}': {e}")
- else:
- print("Invalid mode. Please use 'overwrite', 'add', or 'rewrite'.")
- else:
- print("Invalid command format. Use: edit [[FILE]] [[MODE]]")
- elif command.lower().startswith("read"):
- parts = command.split(maxsplit=1)
- if len(parts) == 2:
- file_name = parts[1]
- file_path = os.path.join(base_directory, file_name)
- if os.path.isfile(file_path):
- try:
- with open(file_path, 'r') as file:
- content = file.read()
- print(f"Contents of '{file_name}':\n{content}")
- except Exception as e:
- print(f"Error reading file '{file_name}': {e}")
- else:
- print(f"The file '{file_name}' does not exist.")
- else:
- print("Invalid command format. Use: read [[FILE]]")
- elif command.lower() == "filecheck":
- try:
- files = [f for f in os.listdir(base_directory) if os.path.isfile(os.path.join(base_directory, f))]
- if files:
- print("Files in the current directory:")
- for file in files:
- size = os.path.getsize(os.path.join(base_directory, file))
- print(f" - {file}: {size} bytes")
- else:
- print("No files found in the current directory.")
- except Exception as e:
- print(f"Error checking files: {e}")
- elif command.lower() == "clear":
- clear_screen()
- print("Screen cleared. Please enter a new command.")
- print(f"Current working directory: {base_directory}")
- else:
- print(f"'{command}' is not recognized. Try again or type 'help' for a list of commands, or 'exit' to quit.")
- open_explorer()
- if __name__ == "__main__":
- main()
Advertisement
Comments
-
- yes, yes i used chatgpt for some of this because i legit started tweaking when i started working on the [[edit]] command
Add Comment
Please, Sign In to add comment