I need help because when i am trying to make my own OS this happened:
command = input('[' location ']$ ')
if command == 'exit':
break
elif command == 'open app':
try:
app = input('App Name: ')
except FileNotFoundError():
print('no such application')
exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' app).read())
else:
print('unkown command: ' command)
Then when I enter an app name that does not exist I expect it to print this: No such application, then I get to try again, but instead this happens:
Traceback (most recent call last):
File "/home/zozijaro/Desktop/ZoziOS/Kernel/terminal/terminal.py", line 11, in <module>
exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' app).read())
FileNotFoundError: [Errno 2] No such file or directory: '/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/app'
maybe someone can fix my code, thx.
CodePudding user response:
You need to have the exec statement inside try block.
CodePudding user response:
Note that the except FileNotFoundError
follows a try
block in which you don't try to open any file, thus the try
block never raises a FileNotFoundError
, and the except
block never gets executed.
You should move the exec(open...
backwards to the try
block.
CodePudding user response:
Replace
try:
app = input('App Name: ')
except FileNotFoundError():
print('no such application')
exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' app).read())
with
try:
exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' app).read())
app = input('App Name: ')
except FileNotFoundError:
print('no such application')
Note that I removed the parenthesis around FileNotFoundError
.