12
Oct
11

custom using the with statement



import os

class MyTempDir(object):
	def __init__(self,to_dir):
		self.to_dir = to_dir
		self.original_dir = os.path.abspath(os.getcwd())

	def __enter__(self):
		os.chdir(self.to_dir)
		return self.to_dir

	def __exit__(self, type, value, traceback):
		os.chdir(self.original_dir)

if __name__ == "__main__":
	with MyTempDir("c:\\") as value:
		print "Inside %s Confirmation %s" % (value,os.getcwd())

Advertisements

0 Responses to “custom using the with statement”



  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

  • 219,796 hits

%d bloggers like this: