欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python獲取當前路徑實現代碼

系統 1767 0

?Python獲取當前路徑實現代碼

import os,sys

使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)

sys.path是Python會去尋找模塊的搜索路徑列表,sys.path[0]和sys.argv[0]是一回事因為Python會自動把sys.argv[0]加入

sys.path。

如果你在C:\test目錄下執行python getpath\getpath.py,那么os.getcwd()會輸出“C:\test”,sys.path[0]會輸出“C:\test\getpath”。

如果你用py2exe模塊把Python腳本編譯為可執行文件,那么sys.path[0]的輸出還會變化:

如果把依賴庫用默認的方式打包為zip文件,那么sys.path[0]會輸出“C:\test\getpath\libarary.zip”;

如果在setup.py里面指定zipfile=None參數,依賴庫就會被打包到exe文件里面,那么sys.path[0]會輸出“C:\test\getpath\getpath.exe”。

            
#!/bin/env python
#-*- encoding=utf8 -*-

import os,sys

if __name__=="__main__":

  print "__file__=%s" % __file__

  print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__)

  print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__))
  
  print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0]  

  print "os.path.abspath(__file__)=%s" % os.path.abspath(__file__)

  print "os.getcwd()=%s" % os.getcwd()

  print "sys.path[0]=%s" % sys.path[0]

  print "sys.argv[0]=%s" % sys.argv[0]


          

輸出結果:

            
D:\>python ./python_test/test_path.py
__file__=./python_test/test_path.py
os.path.realpath(__file__)=D:\python_test\test_path.py
os.path.dirname(os.path.realpath(__file__))=D:\python_test
os.path.split(os.path.realpath(__file__))=D:\python_test
os.path.abspath(__file__)=D:\python_test\test_path.py
os.getcwd()=D:\
sys.path[0]=D:\python_test
sys.argv[0]=./python_test/test_path.py

          

os.getcwd() “D:\”,取的是起始執行目錄

sys.path[0]或sys.argv[0] “D:\python_test”,取的是被初始執行的腳本的所在目錄

os.path.split(os.path.realpath(__file__))[0] “D:\python_test”,取的是__file__所在文件test_path.py的所在目錄?

正確獲取當前的路徑:

            
__file__是當前執行的文件

 

  # 獲取當前文件__file__的路徑

  print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__)

  # 獲取當前文件__file__的所在目錄

  print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__))   
  # 獲取當前文件__file__的所在目錄

  print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0] 


          

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 播放一级黄色片 | 大片毛片| 国产在线看片 | 爱豆在线观看网址91 | 免费 | 高清在线一区二区 | 女人被男人狂躁下面在线观看 | 日本中文字幕网站 | 欧美精品国产制服第一页 | 青青草视频免费观看 | 欧美午夜精品久久久久免费视 | 超级碰在线视频 | www.国产精 | 摸金校尉之九幽将军 | 97精品国产高清在线看入口 | 麻豆高清免费国产一区 | 日本成人在线网站 | av电影免费播放 | 亚洲精品国偷拍自产在线观看蜜桃 | 五月婷婷在线播放 | 天天操天天干天天操 | 精品国产三级 | 粉色视频高清大全免费观看1 | 久久久久一区二区三区 | 久久夏同学国产免费观看 | 天天射天天操天天 | 欧美综合视频 | 国产成人啪精品视频免费网站软件 | 国产精品成人免费一区久久羞羞 | 精品欧美一区视频在线观看 | 免费视频精品一区二区 | 久久久久无码国产精品一区 | 亚洲精品一区在线 | 欧美aaaa视频 | 中国一级特黄毛片大片 | 草草草在线观看 | 亚洲国产精品日韩高清秒播 | 亚洲午夜免费视频 | 欧美亚洲一区二区三区在线 | 99久久99久久 | 亚洲视频免费在线 | 日韩在线观看免费 |