-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhich.py
More file actions
executable file
·48 lines (48 loc) · 1.47 KB
/
which.py
File metadata and controls
executable file
·48 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
from sys import argv
from sys import platform as plat
from sys import exit
from os import environ, listdir, path
def findcmd(command,quiet=True):
'''Searches for a given command in your path
If quiet is set to true, the output will not be displayed
Input:
A command name
Returns/outputs:
A list of commands with path names
Error/abnormal Conditions:
raises AttributeError if the PATH environment variable has no valid entries
raises FileNotFoundError if the command was not found
raises LookupError if no command was supplied
'''
foundcmds=[]
paths=environ.get("PATH")
if(plat=="linux"):
pathsep=":"
dirsep="/"
elif(plat=="windows"):
pathsep=";"
dirsep="\\"
if(paths==None):
raise AttributeError("Cannot find any entries in PATH environment variable.")
else:
paths=paths.split(pathsep)
for dir in paths:
if not (path.exists(dir)):
paths.remove(dir)
filetosearch=''
filetosearch=filetosearch.join([dir,dirsep,command])
if path.exists(filetosearch):
foundcmds.append(filetosearch)
if(len(foundcmds)==0):
raise FileNotFoundError(command," not found")
if(quiet==False):
for cmd in (foundcmds):
print(cmd)
return(foundcmds)
def main():
try:
mycmd=argv[1]
except IndexError:
raise LookupError("No Command Supplied")
findcmd(mycmd)