获取调用Python脚本的快捷方式的目录,可以通过以下方法实现:
os
模块的path
属性获取当前脚本的绝对路径。import os
script_path = os.path.abspath(__file__)
print(script_path)
os
模块的dirname
方法获取当前脚本所在的目录路径。import os
script_dir = os.path.dirname(os.path.abspath(__file__))
print(script_dir)
这样,就可以获取到当前Python脚本所在的目录路径,并将其作为快捷方式,方便在脚本中引用其他文件或资源。
领取专属 10元无门槛券
手把手带您无忧上云