07
Mar
09

python code snippets episode 1



I don’t know if a module exists that acts like the which binary. If you don’t know what which does, I’ll tell you:

  • given an argument ( which represents a binary/executable found on PATH ), which tells you the first location of that binary

Here’s the code I’m using:


import os

def which(program,additional_dirs=[]):
	path = os.environ["PATH"]
	path_components = path.split(":")
	path_components.extend(additional_dirs)
	for item in path_components:
		location = os.path.join(item,program)
		if os.path.exists(location):
			return location
	return None

The code is pretty straight-forward. The function takes a string as the program name, and an optional parameter that represents additional directories to look in. The default behaviour of this functions is to search the PATH variable.

Advertisements

0 Responses to “python code snippets episode 1”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 229,301 hits

%d bloggers like this: