forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_cheat.py
More file actions
executable file
·98 lines (65 loc) · 2.05 KB
/
path_cheat.py
File metadata and controls
executable file
·98 lines (65 loc) · 2.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
"""
## path
os.path vs os:
http://stackoverflow.com/questions/2724348/should-i-use-import-os-path-or-import-os
"""
import os.path
import tempfile
if '## join':
assert os.path.join('a', 'b', 'c') == 'a{s}b{s}c'.format(s=os.sep)
os.path.join('a//', '/b')
if '## split ## splitext':
path = os.path.join('a', 'b', 'c.e')
root, basename = os.path.split(path)
basename_noext, ext = os.path.splitext(basename)
assert root == os.path.join('a', 'b')
assert basename_noext == 'c'
assert ext == '.e'
if '## exists':
# Returns False for broken symlinks.
# lexists returns True in that case.
temp = tempfile.NamedTemporaryFile()
assert os.path.exists(temp.name)
temp.close()
assert not os.path.exists(temp.name)
temp = tempfile.mkdtemp()
assert os.path.exists(temp)
os.rmdir(temp)
if '## isfile':
# Exists and is file, not directory.
temp = tempfile.NamedTemporaryFile()
assert os.path.isfile(temp.name)
temp.close()
assert not os.path.isfile(temp.name)
temp = tempfile.mkdtemp()
assert not os.path.isfile(temp)
os.rmdir(temp)
if '## isdir':
# Exists and is directory.
temp = tempfile.NamedTemporaryFile()
assert not os.path.isdir(temp.name)
temp.close()
temp = tempfile.mkdtemp()
assert os.path.isdir(temp)
os.rmdir(temp)
assert not os.path.isdir(temp)
if '## islink':
# Detect if given path is a symbolic link.
# TODO example.
os.path.islink('/a')
if '## abspath':
# Absolute path:
print('os.path.abspath(u\'.\') = ' + os.path.abspath(u'.'))
if '## relpath':
# Absolute path resolving links recursively:
os.path.relpath(u'/a')
if '## commonprefix':
assert os.path.commonprefix([
'{s}a{s}b{s}c{s}d'.format(s=os.sep),
'{s}a{s}b{s}e{s}d'.format(s=os.sep)
]) == '{s}a{s}b{s}'.format(s=os.sep)
def isparent(path1, path2):
return os.path.commonprefix([path1, path2]) == path1
def ischild(path1, path2):
return os.path.commonprefix([path1, path2]) == path2