42 lines
618 B
Plaintext
42 lines
618 B
Plaintext
|
|
class Open_File():
|
|
|
|
def __init__(self, destination):
|
|
pass
|
|
|
|
def __enter__(self):
|
|
pass
|
|
|
|
def __exit__(self, exc_type, exc_val, traceback):
|
|
pass
|
|
|
|
|
|
#### Using contextlib ####
|
|
|
|
@contextmanager
|
|
def open_file(file, mode):
|
|
f = open(file, mode)
|
|
yield f
|
|
f.close()
|
|
|
|
|
|
with open_file('sample.txt', 'w') as f:
|
|
f.write('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
|
|
|
|
print(f.closed)
|
|
|
|
|
|
#### CD Example ####
|
|
|
|
cwd = os.getcwd()
|
|
os.chdir('Sample-Dir-One')
|
|
print(os.listdir())
|
|
os.chdir(cwd)
|
|
|
|
cwd = os.getcwd()
|
|
os.chdir('Sample-Dir-Two')
|
|
print(os.listdir())
|
|
os.chdir(cwd)
|
|
|
|
|