美文网首页
[Python] 获取当前路径

[Python] 获取当前路径

作者: DexterLei | 来源:发表于2017-10-12 18:46 被阅读438次
  • sys.argv[0]

import sys
print sys.argv[0]#获得的是当前执行脚本的位置(若在命令行执行的该命令,则为空)
>>> F:/SEG/myResearch/myProject_2/test.py
  • os模块

import os
print os.getcwd()#获得当前工作目录
print os.path.abspath('.')#获得当前工作目录
print os.path.abspath('..')#获得当前工作目录的父目录
print os.path.abspath(os.curdir)#获得当前工作目录
>>> F:\SEG\myResearch\myProject_2 
>>> F:\SEG\myResearch\myProject_2 
>>> F:\SEG\myResearch 
>>> F:\SEG\myResearch\myProject_2
参考资料:

相关文章

网友评论

      本文标题:[Python] 获取当前路径

      本文链接:https://www.haomeiwen.com/subject/numoyxtx.html