如需转载请注明出处。
win10 64位、Python 3.6.3、Sublime Text 3,whichcraft 0.5.2(2018-10-09)。
whichcraft,它提供跨平台(Linux、Mac、Windows)、跨Python的shutil.which
功能。
其中,shutil是shell utility的缩写,是Python 3的一个标准库。它是一个高级文件操作(如:拷贝、移动、压缩、解压)模块。
Python os模块 提供了对目录/文件的新建/删除/查看文件属性,以及对文件/目录的路径操作(如:绝对路径、父目录等)。但,文件操作还应包括:拷贝、移动、压缩、解压。
因此,shutil
模块应运而生,对os中文件操作提供补充。
PS:whichcraft 是shutil.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
参考:
1、官方文档
2、shuitl.which–Python3文档
如需转载请注明出处。