Working with paths

Now, we are going to learn about os.path(). It is used for path manipulations. In this section, we will look at some of the functions that the os module offers for pathnames.

Start the python3 console:

student@ubuntu:~$ python3
Python 3.6.6 (default, Sep 12 2018, 18:26:19)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>
>>> import os
>>> os.path.abspath('sample.txt')
'/home/student/work/sample.txt'
>>> os.path.dirname('/home/student/work/sample.txt')
'/home/student/work'
>>> os.path.basename('/home/student/work/sample.txt')
'sample.txt'
>>> os.path.exists('/home/student/work/sample.txt')
True
>>> os.path.getsize('/home/student/work/sample.txt')
39
>>> os.path.isfile('/home/student/work/sample.txt')
True
>>> os.path.isdir('/home/student/work/sample.txt')
False