1. os.getcwd()
只有路径,不包含py文件名
path=os.getcwd() print(path) #结果:E:\pyworkspace\pycharmProjects\cdtest\oproject\Pubic
2.os.path.realpath(__file__)
#包含了py文件
path=os.path.realpath(__file__) print(path) #结果:E:\pyworkspace\pycharmProjects\cdtest\oproject\Pubic\test.py
3. cwd = os.path.split(os.path.realpath(__file__))
cwd = os.path.split(os.path.realpath(__file__)) print(cwd) #结果:('E:\\pyworkspace\\pycharmProjects\\cdncp-ta\\ocr\\Pub', 'ss.py') #返回一个数组,第一个数据是path,第二个数据是py文件名
path= os.path.split(os.path.realpath(__file__))[0] print(path)
#结果:E:\pyworkspace\pycharmProjects\cdtest\oproject\Pubic
标签:__,realpath,路径,python,py,file,path,os From: https://www.cnblogs.com/pingguomang/p/17968165