-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path014with.py
61 lines (50 loc) · 1.33 KB
/
014with.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
#py3.7
with open('hello_foo.txt', 'w') as f:
f.write('hello, world!')
#same as:
# f = opent('hello_foo.txt', 'w')
# try:
# f.wtite('hello, world')
# finally:
# f.close()
with open('hello_foo.txt', 'r') as f:
print(f.read())
print()
#more advanced:
from contextlib import contextmanager
import os
@contextmanager # https://stackoverflow.com/a/3012921
def working_directory(path):
current_dir = os.getcwd()
os.chdir(path)
try:
yield
except:
print(f"directory {path} not found")
finally:
os.chdir(current_dir)
#Pavel: enclosed with block to get rid of error if directory is not found
#should think later if possible to handle such things inside "with" block
try:
with working_directory("data/stuff"):
# do something within data/stuff
print('Hi')
except:
print('problem with with of directory data/stuff')
finally:
print('first try final')
# here I am back again in the original working directory
print()
#Another way: https://stackoverflow.com/a/5205878/5233335
try:
f = open('foo.txt')
except IOError:
print('error open "foo.txt"')
else:
with f:
print('foo opened')
print(f.readlines())
#
# some_code
#