Skip to content

Commit 9057552

Browse files
Add files via upload
1 parent 6bdb581 commit 9057552

1 file changed

Lines changed: 217 additions & 0 deletions

File tree

linux_assistant.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import os
2+
import shutil
3+
from datetime import datetime
4+
import webbrowser
5+
6+
# Command Explanations for Quick Help
7+
command_help = {
8+
"ls": "Lists directory contents.",
9+
"cat": "Displays the contents of a file.",
10+
"cp": "Copies files or directories.",
11+
"mv": "Moves files or directories.",
12+
"rm": "Deletes files or directories.",
13+
"mkdir": "Creates a new directory.",
14+
"chmod": "Changes file permissions.",
15+
}
16+
17+
def list_files():
18+
"""List all files in the current directory."""
19+
print("\nFiles in the current directory:")
20+
for file in os.listdir('.'):
21+
print(file)
22+
print()
23+
24+
def delete_file():
25+
"""Delete a specified file."""
26+
filename = input("Enter the name of the file to delete: ")
27+
if os.path.isfile(filename):
28+
os.remove(filename)
29+
print(f"File '{filename}' deleted successfully.")
30+
else:
31+
print(f"File '{filename}' does not exist.")
32+
33+
def view_file():
34+
"""View the contents of a specified file."""
35+
filename = input("Enter the name of the file to view: ")
36+
if os.path.isfile(filename):
37+
with open(filename, 'r') as file:
38+
content = file.read()
39+
print(f"\nContents of '{filename}':\n{content}\n")
40+
else:
41+
print(f"File '{filename}' does not exist.")
42+
43+
def search_in_file():
44+
"""Search for a term in a specified file."""
45+
filename = input("Enter the name of the file to search in: ")
46+
if os.path.isfile(filename):
47+
search_term = input("Enter the term to search for: ")
48+
with open(filename, 'r') as file:
49+
content = file.readlines()
50+
found = any(search_term in line for line in content)
51+
if found:
52+
print(f"The term '{search_term}' was found in '{filename}'.")
53+
else:
54+
print(f"The term '{search_term}' was NOT found in '{filename}'.")
55+
else:
56+
print(f"File '{filename}' does not exist.")
57+
58+
def create_directory():
59+
"""Create a new directory."""
60+
dirname = input("Enter the name of the directory to create: ")
61+
if not os.path.exists(dirname):
62+
os.makedirs(dirname)
63+
print(f"Directory '{dirname}' created successfully.")
64+
else:
65+
print(f"Directory '{dirname}' already exists.")
66+
67+
def copy_file():
68+
"""Copy a specified file."""
69+
src_filename = input("Enter the name of the file to copy: ")
70+
if os.path.isfile(src_filename):
71+
dest_filename = input("Enter the name of the new file: ")
72+
shutil.copy(src_filename, dest_filename)
73+
print(f"File '{src_filename}' copied to '{dest_filename}' successfully.")
74+
else:
75+
print(f"File '{src_filename}' does not exist.")
76+
77+
def move_file():
78+
"""Move a specified file."""
79+
src_filename = input("Enter the name of the file to move: ")
80+
if os.path.isfile(src_filename):
81+
dest_directory = input("Enter the destination directory: ")
82+
if os.path.exists(dest_directory):
83+
shutil.move(src_filename, os.path.join(dest_directory, src_filename))
84+
print(f"File '{src_filename}' moved to '{dest_directory}' successfully.")
85+
else:
86+
print(f"Destination directory '{dest_directory}' does not exist.")
87+
else:
88+
print(f"File '{src_filename}' does not exist.")
89+
90+
def rename_file():
91+
"""Rename a specified file."""
92+
old_filename = input("Enter the current name of the file: ")
93+
if os.path.isfile(old_filename):
94+
new_filename = input("Enter the new name of the file: ")
95+
os.rename(old_filename, new_filename)
96+
print(f"File '{old_filename}' renamed to '{new_filename}' successfully.")
97+
else:
98+
print(f"File '{old_filename}' does not exist.")
99+
100+
def show_help(command):
101+
"""Show help for a specific command."""
102+
if command in command_help:
103+
print(f"{command}: {command_help[command]}")
104+
else:
105+
print(f"No help available for '{command}'.")
106+
107+
def check_permissions():
108+
"""Check permissions of a specified file."""
109+
filename = input("Enter the name of the file to check permissions: ")
110+
if os.path.isfile(filename):
111+
permissions = oct(os.stat(filename).st_mode)[-3:]
112+
print(f"Permissions for '{filename}': {permissions}")
113+
else:
114+
print(f"File '{filename}' does not exist.")
115+
116+
def open_web():
117+
"""Open a URL in the default web browser."""
118+
url = input("Enter the URL you want to open: ")
119+
webbrowser.open(url)
120+
print(f"Opening {url} in your web browser.")
121+
122+
def display_commands_summary(used_commands):
123+
"""Display summary of frequently used commands."""
124+
print("\nSummary of commands used in this session:")
125+
for command, count in used_commands.items():
126+
print(f"{command}: used {count} time(s)")
127+
print("Thanks for using computer_Terminal!")
128+
129+
# Main program starts here
130+
current_time_local = datetime.now()
131+
print("computer_Terminal")
132+
print("Version 1.0")
133+
print("Bytesec Inc.")
134+
print()
135+
print("Current Time:", current_time_local.strftime("%Y-%m-%d %I:%M %p"))
136+
137+
used_commands = {}
138+
139+
while True:
140+
answer = input(
141+
"\nWhat do you want to do today?\n"
142+
" a. Make file\n"
143+
" b. Add to file\n"
144+
" c. List files\n"
145+
" d. Delete file\n"
146+
" e. View file\n"
147+
" f. Search in file\n"
148+
" g. Create directory\n"
149+
" h. Copy file\n"
150+
" i. Move file\n"
151+
" j. Rename file\n"
152+
" k. Check permissions\n"
153+
" l. Open Web\n"
154+
" m. Help\n"
155+
" n. Exit\n"
156+
).strip().lower()
157+
158+
# Increment command usage for summary
159+
used_commands[answer] = used_commands.get(answer, 0) + 1
160+
161+
if answer == 'a':
162+
filename = input("Enter the name of the file to create: ")
163+
with open(filename, 'w') as file:
164+
file.write("This is a new file.\n")
165+
print(f"File '{filename}' created successfully.")
166+
167+
elif answer == 'b':
168+
filename = input("Enter the name of the file to add to: ")
169+
if os.path.isfile(filename):
170+
content = input("Enter content to add to the file: ")
171+
with open(filename, 'a') as file:
172+
file.write(content + '\n')
173+
print(f"Content added to '{filename}' successfully.")
174+
else:
175+
print(f"File '{filename}' does not exist. Please create it first.")
176+
177+
elif answer == 'c':
178+
list_files()
179+
180+
elif answer == 'd':
181+
delete_file()
182+
183+
elif answer == 'e':
184+
view_file()
185+
186+
elif answer == 'f':
187+
search_in_file()
188+
189+
elif answer == 'g':
190+
create_directory()
191+
192+
elif answer == 'h':
193+
copy_file()
194+
195+
elif answer == 'i':
196+
move_file()
197+
198+
elif answer == 'j':
199+
rename_file()
200+
201+
elif answer == 'k':
202+
check_permissions()
203+
204+
elif answer == 'l':
205+
open_web()
206+
207+
elif answer == 'm':
208+
command = input("Enter the command you need help with: ")
209+
show_help(command)
210+
211+
elif answer == 'n':
212+
display_commands_summary(used_commands)
213+
print("Exiting the program.")
214+
break
215+
216+
else:
217+
print("Invalid option. Please choose a valid option.")

0 commit comments

Comments
 (0)