Skip to content

Instantly share code, notes, and snippets.

@major
Created August 21, 2014 19:51
Show Gist options
  • Select an option

  • Save major/2de0053313bc515b1e30 to your computer and use it in GitHub Desktop.

Select an option

Save major/2de0053313bc515b1e30 to your computer and use it in GitHub Desktop.
ansible #8640/#8642
[root@builder ~]# python test.py /var/www/html
#8642 ----->
/var
/var/www
/var/www/html
#8640 ----->
/
//var
//var/www
//var/www/html
[root@builder ~]# python test.py test_dir
#8642 ----->
test_dir
#8640 ----->
/
//root
//root/test_dir
import os
import sys
path = sys.argv[1]
print "#8642 ----->"
curpath = ''
for dirname in path.strip('/').split('/'):
curpath = '/'.join([curpath, dirname])
if not os.path.isabs(path):
curpath = curpath.lstrip('/')
print curpath
print "#8640 ----->"
curpath = ''
for dirname in os.path.realpath(path).split('/'):
curpath = '/'.join([curpath, dirname])
print curpath
@jimi-c
Copy link
Copy Markdown

jimi-c commented Aug 21, 2014

Adding this test:

print "# proper join/split ----->"
curpath = ''
for dirname in os.path.split(os.path.realpath(path)):
    curpath = os.path.join(curpath, dirname)
    print curpath

Output:

# python test_major.py test_dir
#8642 ----->
test_dir
#8640 ----->
/
//root
//root/testing
//root/testing/test_dir
# proper join/split ----->
/root/testing
/root/testing/test_dir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment