Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created April 4, 2016 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhijitmamarde/415fe7b5a71981b5de250ccafd5a72bc to your computer and use it in GitHub Desktop.
Save abhijitmamarde/415fe7b5a71981b5de250ccafd5a72bc to your computer and use it in GitHub Desktop.
Search a file from environment PATH
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import sys
import os.path
path_dirs = os.getenv('PATH').split(':')
cmd = sys.argv[1]
found_in_dirs = []
print "Path:\n\t%s" % "\n\t".join(path_dirs)
for path_dir in path_dirs:
for files in os.listdir(path_dir):
if cmd == files:
if path_dir not in found_in_dirs:
found_in_dirs.append(path_dir)
if len(found_in_dirs):
print "\nCommand '%s' found in:\n\t%s" % (cmd, "\n\t".join(found_in_dirs))
else:
print "\nCommand '%s' not found in path!" % cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment