python中关于文件路径的操作

1、sys.path[0]  返回调用python解释器的脚本所在的目录
例:  HOME = sys.path[0]
           HOME = '/home/jia/ssd/demo/live.py'

 2、os.path.dirname()  返回当前文件的路径

例:HOME1 = os.path.dirname(HOME)

         HOME1 = '/home/jia/ssd/demo/'

3、巧妙利用os.path.dirname()获取上级路径

例:HOME2 = os.path.dirname(HOME1)

         HOME2 = '/home/jia/ssd/'

4、replace函数替换字符串

例:label_path = 'data/VOCdevkit2007/VOC2007/SegmentationClass/009950.png'

   image_path = label_path.replace('SegmentationClass', 'JPEGImages')
   image_path = image_path.replace('png', 'jpg')

        image_path = 'data/VOCdevkit2007/VOC2007/JPEGImages/009950.jpg'

5、glob函数返回所有匹配的文件路径列表(list)

例:glob.glob(os.path.join(data/VOCdevkit2007/VOC2007/, 'SegmentationClass/*.png'))

 

 

参考:https://www.runoob.com/python/python-os-path.html

 

你可能感兴趣的:(#,python)