获取当前脚本所在的绝对路径
os.path.split(os.path.realpath(__file__))[0]
详解:
__file__:python内部变量,显示文件在当前搜索路径下的相对路径与文件名
os.path.realpath():获取文件的绝对路径
os.path.split():拆分最后一个路径后的内容,形成一个List对象
[0]:取列表对象的第一个参数,即为路径
测试程序:
import osprint(__file__)print(os.path.realpath(__file__))pwd=os.path.split(os.path.realpath(__file__))print(pwd) |

浙公网安备 33010602011771号