一、路径处理
python里面的OS模块有许多方法让我们通过代码实现代码创建,删除和更改目录。
os模块:Python内置的模块,是python和操作系统进行交互的一个模块
二、魔法变量
__ file __
# __file__:获取当前文件的绝对路径
print(__file__)
# 运行结果:H:/python2022/09/路径处理.py
__ name __
# __name__:
# 1.如果当前文件值程序的启动文件,即在当前文件中运行结果是__main__
# 2.如果不在启动文件中,代表的就是所在文件(模块)的模块名
print("当前运行文件中的__name__:",__name__)
# 运行结果:__main__
import ostest
ostest.func()
# file:ostest.py
def func():
print("ostest中的__name__:",__name__)
运行结果:
当前运行文件中的__name__: main
ostest中的__name__: ostest
小知识:
导入一个模块是默认把该模块的代码从上往下执行:
# file:ostest.py
def func():
print("ostest