Skip to content

Commit f277136

Browse files
committed
add ospath
1 parent 3d67b1e commit f277136

15 files changed

Lines changed: 190 additions & 0 deletions

ospath/example/one/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
contents

ospath/example/two.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
contents

ospath/ospath_abspath.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Compute an absolute path from a relative path.
5+
"""
6+
7+
import os.path
8+
9+
for path in ['.', '..', './one/two/three', '../one/two/three']:
10+
print '"%s" : "%s"' % (path, os.path.abspath(path))

ospath/ospath_basename.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Determine the base filename from a path.
5+
"""
6+
7+
import os.path
8+
9+
for path in ['/one/two/three',
10+
'/one/two/three/',
11+
'/',
12+
'.',
13+
'']:
14+
print '"%s" : "%s"' % (path, os.path.basename(path))

ospath/ospath_commonprefix.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Find the prefix string common to a group of paths.
5+
"""
6+
7+
import os.path
8+
9+
paths = ['/one/two/three/four',
10+
'/one/two/threefold',
11+
'/one/two/three/',
12+
]
13+
print paths
14+
print os.path.commonprefix(paths)

ospath/ospath_dirname.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Find the directory portion of a filename.
5+
"""
6+
7+
import os.path
8+
9+
for path in ['/one/two/three',
10+
'/one/two/three/',
11+
'/',
12+
'.',
13+
'']:
14+
print '"%s" : "%s"' % (path, os.path.dirname(path))

ospath/ospath_expanduser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Expand tilde in filenames.
5+
"""
6+
7+
import os.path
8+
9+
for user in ['', 'dhellmann', 'postgres']:
10+
lookup = '~' + user
11+
print lookup, ':', os.path.expanduser(lookup)

ospath/ospath_expandvars.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Expand shell variables in filenames.
5+
"""
6+
7+
import os.path
8+
import os
9+
10+
os.environ['MYVAR'] = 'VALUE'
11+
12+
print os.path.expandvars('/path/to/$MYVAR')

ospath/ospath_join.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Combine path components to create a single path.
5+
"""
6+
7+
import os.path
8+
9+
for parts in [('one', 'two', 'three'),
10+
('/', 'one', 'two', 'three'),
11+
('/one', '/two', '/three'),
12+
]:
13+
print parts, ':', os.path.join(*parts)

ospath/ospath_normpath.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""Compute a "normalized" path.
5+
"""
6+
7+
import os.path
8+
9+
for path in ['one//two//three',
10+
'one/./two/./three',
11+
'one/../one/two/three',
12+
]:
13+
print path, ':', os.path.normpath(path)

0 commit comments

Comments
 (0)