Add os.path · lyonslo/python-api-tesing@69db9b2 · GitHub
Skip to content

Commit 69db9b2

Browse files
author
yangshifu
committed
Add os.path
1 parent e68ebcf commit 69db9b2

14 files changed

Lines changed: 682 additions & 0 deletions
Lines changed: 57 additions & 0 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# CreateDate: 2018-1-29
5+
# https://pymotw.com/3/os.path/index.html
6+
# ospath_basename.py
7+
8+
import os.path
9+
10+
PATHS = [
11+
'/one/two/three',
12+
'/one/two/three/',
13+
'/',
14+
'.',
15+
'',
16+
]
17+
18+
for path in PATHS:
19+
print('{!r:>17} : {!r}'.format(path, os.path.basename(path)))
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+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# CreateDate: 2018-1-29
5+
# https://pymotw.com/3/os.path/index.html
6+
# ospath_commonpath.py
7+
8+
import os.path
9+
10+
paths = ['/one/two/three/four',
11+
'/one/two/threefold',
12+
'/one/two/three/',
13+
]
14+
for path in paths:
15+
print('PATH:', path)
16+
17+
print()
18+
print('PREFIX:', os.path.commonpath(paths))
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+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# CreateDate: 2018-1-29
5+
# https://pymotw.com/3/os.path/index.html
6+
# ospath_commonprefix.py
7+
8+
import os.path
9+
10+
paths = ['/one/two/three/four',
11+
'/one/two/threefold',
12+
'/one/two/three/',
13+
]
14+
for path in paths:
15+
print('PATH:', path)
16+
17+
print()
18+
print('PREFIX:', os.path.commonprefix(paths))
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+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# CreateDate: 2018-1-29
5+
# https://pymotw.com/3/os.path/index.html
6+
# ospath_dirname.py
7+
8+
import os.path
9+
10+
PATHS = [
11+
'/one/two/three',
12+
'/one/two/three/',
13+
'/',
14+
'.',
15+
'',
16+
]
17+
18+
for path in PATHS:
19+
print('{!r:>17} : {!r}'.format(path, os.path.dirname(path)))
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+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# CreateDate: 2018-1-29
5+
# https://pymotw.com/3/os.path/index.html
6+
# ospath_expanduser.py
7+
8+
import os.path
9+
10+
for user in ['', 'andrew', 'nosuchuser']:
11+
lookup = '~' + user
12+
print('{!r:>15} : {!r}'.format(
13+
lookup, os.path.expanduser(lookup)))
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+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# CreateDate: 2018-1-29
5+
# https://pymotw.com/3/os.path/index.html
6+
# ospath_expandvars.py
7+
8+
import os.path
9+
import os
10+
11+
os.environ['MYVAR'] = 'VALUE'
12+
13+
print(os.path.expandvars('/path/to/$MYVAR'))
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+
Lines changed: 48 additions & 0 deletions

0 commit comments

Comments
 (0)