欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置

 更新時(shí)間:2020年03月09日 14:24:12   作者:潘高  
這篇文章主要介紹了使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

獲取當(dāng)前工作目錄

import sys

print(sys.path[0])

獲取執(zhí)行命令的位置

import os

print(os.getcwd())

補(bǔ)充知識(shí):Python獲取當(dāng)前執(zhí)行文件,根據(jù)某一級(jí)目錄名稱(chēng),獲取此目錄名稱(chēng)所在的絕對(duì)路徑

假如當(dāng)前文件絕對(duì)路徑:E:\learn\python\我的file\my.py

#coding:utf-8
import os
 
#dirName:上級(jí)目錄名稱(chēng)
#sysCoding:系統(tǒng)編碼格式
#targetCoding:轉(zhuǎn)換目標(biāo)編碼格式
def get_dir_realpath(dirName,sysCoding,targetCoding):
  path = os.path.split(os.path.realpath(__file__))[0].decode(sysCoding).encode(targetCoding)
  dirList = path.split("\\")
  length = len(dirList)
  for _ in range(1,length):
    fileName = os.path.split(path)[1]
    path = os.path.split(path)[0]
    if fileName == dirName:
      return path
      break
  return ""
    
print get_dir_realpath("我的file",'cp936',"utf-8") 執(zhí)行結(jié)果:E:\learn\python

print get_dir_realpath("python",'cp936',"utf-8") 執(zhí)行結(jié)果:E:\learn

以上這篇使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論