Absolute path python

os.path.abspath makes sure a path is absolute. In the following example we are iterating through a directory and printing absolute path of each file in that directory.

Example

# Import the os module
# os.walk and  os.path are in this module 
import os
  
# Set the directory you want to start from
rootDir = 'D:\\wordMeaningNew'

for dirpath,_,filenames in os.walk(rootDir):
    for f in filenames:
        #getting full path
        fullPath = os.path.abspath(os.path.join(dirpath, f))
        
        # print full path
        print('\t%s' % fullPath)

Output

	D:\wordMeaningNew\vocabulary.doc
	D:\wordMeaningNew\vocabulary.pdf
	D:\wordMeaningNew\newFolder\words.pdf