Tutorial by Examples

To join two or more path components together, firstly import os module of python and then use following: import os os.path.join('a', 'b', 'c') The advantage of using os.path is that it allows code to remain compatible over all operating systems, as this uses the separator appropriate for the pl...
Use os.path.abspath: >>> os.getcwd() '/Users/csaftoiu/tmp' >>> os.path.abspath('foo') '/Users/csaftoiu/tmp/foo' >>> os.path.abspath('../foo') '/Users/csaftoiu/foo' >>> os.path.abspath('/foo') '/foo'
To split one component off of the path: >>> p = os.path.join(os.getcwd(), 'foo.txt') >>> p '/Users/csaftoiu/tmp/foo.txt' >>> os.path.dirname(p) '/Users/csaftoiu/tmp' >>> os.path.basename(p) 'foo.txt' >>> os.path.split(os.getcwd()) ('/Users/csafto...
os.path.abspath(os.path.join(PATH_TO_GET_THE_PARENT, os.pardir))
to check if the given path exists path = '/home/john/temp' os.path.exists(path) #this returns false if path doesn't exist or if the path is a broken symbolic link
to check if the given path is a directory dirname = '/home/john/python' os.path.isdir(dirname) to check if the given path is a file filename = dirname + 'main.py' os.path.isfile(filename) to check if the given path is symbolic link symlink = dirname + 'some_sym_link' os.path.islink(symli...

Page 1 of 1