1. 程式人生 > >Python3.X whichcraft包詳解:返回可執行檔案的路徑

Python3.X whichcraft包詳解:返回可執行檔案的路徑

如需轉載請註明出處。 win10 64位、Python 3.6.3、Sublime Text 3,whichcraft 0.5.2(2018-10-09)。

whichcraft,它提供跨平臺(Linux、Mac、Windows)、跨Python的shutil.which功能。

其中,shutilshell utility的縮寫,是Python 3的一個標準庫。它是一個高階檔案操作(如:拷貝、移動、壓縮、解壓)模組。 Python os模組 提供了對目錄/檔案的新建/刪除/檢視檔案屬性,以及對檔案/目錄的路徑操作(如:絕對路徑、父目錄等)。但,檔案操作還應包括:拷貝、移動、壓縮、解壓。 因此,shutil模組應運而生,對os中檔案操作提供補充。

PS:whichcraftshutil.which函式的一個鋪墊,shutil.which是為在多個Python版本和Windows內部工作而設計。適合Python 2.x的程式碼是基於Python 3原始碼中提取的。作者最初是為了Cookiecutter做的,但為了減少Cookiecutter專案的line count,從而把該功能提取出來。

其中,Cookiecutter(譯作 “餅乾切割機”)是一個從cookiecutter(專案模板)建立專案的命令列工具。如:從Python包專案模板中建立Python包專案。

shutil.which()函式的作用是:如果呼叫了給定的cmd,返回一個可執行的可執行檔案的路徑。如果沒有cmd被呼叫,則返回None

shutil.which(cmd, mode=os.F_OK | os.X_OK, path=None)引數解釋: mode是一個傳遞給os.access()的許可掩碼,預設情況下,它確定檔案是否存在和可執行; path 如果沒有指定,則將使用os.environ()的結果,即返回PATH值 或os.depath的一個fallback。

另: 在Windows下,無論是否使用了預設目錄或自己提供的目錄,當前目錄總是在路徑的前面,這是shell在查詢可執行檔案時使用的行為。此外,當在路徑中找到cmd時,將檢查PATHEXT環境變數。例如,若呼叫shutil.which("python")which()

將搜尋PATHEXT,從而知道它應該在路徑目錄中查詢python.exe。例在Windows下:

>>> import shutil
>>> shutil.which("python")
'D:\\ATP\\venv\\Scripts\\python.EXE'

當然,如上我是在虛擬目錄下。這是Python 3.3以上版本才有的功能。

綜上所述: whichcraft包 就是shutil.which()函式的“副本”,作用跟shutil.which()是相同的,將返回可行檔案的路徑(呼叫了給定cmd時)或返回None(未呼叫cmd時)

用法:

>>> from whichcraft import which
>>> which('date')
>>> which('adb')
'D:\\adt\\sdk\\platform-tools\\adb.EXE'
>>> which('python')
'D:\\ATP\\venv\\Scripts\\python.EXE'
>>> which("a-made-up-name") is None
True
>>> which("python") is None
False

whichcraft.py原始碼:

# -*- coding: utf-8 -*-

__author__ = "Daniel Roy Greenfeld"
__email__ = "[email protected]"
__version__ = "0.5.2"

import os
import sys

try:  # Forced testing
    from shutil import which
except ImportError:  # Forced testing
    # Versions prior to Python 3.3 don't have shutil.which

    def which(cmd, mode=os.F_OK | os.X_OK, path=None):
        """Given a command, mode, and a PATH string, return the path which
        conforms to the given mode on the PATH, or None if there is no such
        file.
        `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
        of os.environ.get("PATH"), or can be overridden with a custom search
        path.
        Note: This function was backported from the Python 3 source code.
        """
        # Check that a given file can be accessed with the correct mode.
        # Additionally check that `file` is not a directory, as on Windows
        # directories pass the os.access check.

        def _access_check(fn, mode):
            return os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn)

        # If we're given a path with a directory part, look it up directly
        # rather than referring to PATH directories. This includes checking
        # relative to the current directory, e.g. ./script
        if os.path.dirname(cmd):
            if _access_check(cmd, mode):
                return cmd

            return None

        if path is None:
            path = os.environ.get("PATH", os.defpath)
        if not path:
            return None

        path = path.split(os.pathsep)

        if sys.platform == "win32":
            # The current directory takes precedence on Windows.
            if os.curdir not in path:
                path.insert(0, os.curdir)

            # PATHEXT is necessary to check on Windows.
            pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
            # See if the given file matches any of the expected path
            # extensions. This will allow us to short circuit when given
            # "python.exe". If it does match, only test that one, otherwise we
            # have to try others.
            if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
                files = [cmd]
            else:
                files = [cmd + ext for ext in pathext]
        else:
            # On other platforms you don't have things like PATHEXT to tell you
            # what file suffixes are executable, so just pass on cmd as-is.
            files = [cmd]

        seen = set()
        for dir in path:
            normdir = os.path.normcase(dir)
            if normdir not in seen:
                seen.add(normdir)
                for thefile in files:
                    name = os.path.join(dir, thefile)
                    if _access_check(name, mode):
                        return name

        return None

如需轉載請註明出處。