Python获取当前目录的方法总结

测试 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys

print(__file__)
print(os.path.realpath(__file__))
print('using sys.executable:', repr(os.path.dirname(os.path.realpath(sys.executable))))
print('using sys.argv[0]:', repr(os.path.dirname(os.path.realpath(sys.argv[0]))))
print(sys.argv[0])
print(sys.path[0])
print(os.getcwd())
input("请输入任意键继续")

  ↑↑↑以上为测试代码↑↑↑

源码路径为D:\TestCode\testPath.py

exe文件路径为D:\TestCode\dist\testPath.exe


结果

0x00

命令行进入源码目录下运行

命令:

D:\TestCode>python testPath.py

结果:

testPath.py
D:\TestCode\testPath.py
using sys.executable: 'C:\\Users\\LQT\\AppData\\Local\\Programs\\Python\\Python37'
using sys.argv[0]: 'D:\\TestCode'
testPath.py
D:\TestCode
D:\TestCode

0x01

命令行进入非源码目录下运行

命令:

E

你可能感兴趣的:(Python,python,当前目录)