import os
# 获取当前洞府坐标 (当前路径)
print(os.getcwd())
# 建立分舵 (新建文件夹)
os.mkdir("Python宗门")
# 进入分舵
os.chdir("Python宗门")
# 探查分舵内所有宝物 (列出文件)
print(os.listdir("."))
不同位面 (Windows/Mac) 的路径写法不同。用 os.path 可无视位面差异。
# 自动拼接路径 (Windows用 \, Mac用 /)
path = os.path.join("Python宗门", "秘籍.txt")
# 判断宝物是否存在
if os.path.exists(path):
print("找到秘籍!")
任务:召唤 os,并打印出你当前的操作系统名称 (os.name)。