系统:Windows 7 语言版本:Anaconda3-4.3.0.1-Windows-x86_64 编辑器:pycharm-community-2016.3.2
import osprint(os.path.abspath(__file__))
current_address_1 = os.path.dirname(os.path.abspath(__file__))current_address_2 = os.path.abspath('.')
current_address_3 = os.getcwd()
print(current_address_1)
print(current_address_2)
print(current_address_3)print("\n切换工作目录")
os.chdir(r"E:\Coding\E_PythonWriting")
print(os.path.abspath(__file__))
current_address_1 = os.path.dirname(os.path.abspath(__file__))current_address_2 = os.path.abspath('.')
current_address_3 = os.getcwd()
print(current_address_1)
print(current_address_2)
print(current_address_3)
运行结果
代码截图
os.path.abspath(.)
及os.getcwd()
获取的是当前工作目录所在文件夹,并不是该py文件所在文件夹os.chdir
是用来切换当前工作目录的注意:大部分的时候三种方法执行结果是一样的,但个别情况下是不同的,根据自己的真实需求,选择需要的方法