便携式pwd(打印工作目录)。
pwdpp的Python项目详细描述
“pwdpp”是用python编写的可移植pwd(打印工作目录)。
要求
- python 2.7或更高版本
- psutil如果使用正式的cpython windows二进制文件。
用法
you@yourhost: ~$ pwdpp --help usage: pwdpp [-h] [-P] [-L] [-s {bs,fs}] [-W] Print the full filename of the current working directory. optional arguments: -h, --help show this help message and exit -P, --physical avoid all symlinks (default). (if your system can reveal symlinks.) -L, --logical use PWD from environment, even if it contains symlinks. (if your system can't detect symlinks, this option has no effect.) -s {bs,fs}, --pathsep {bs,fs} specify path separator. (bs: backslash / fs: forward slash.) -W print the Win32 value of the physical directory. this is a simple emulation of bash's pwd on MSYS(2). Of course, this makes no sense on *nix.
您可能需要设置pythonionecoding环境变量,例如'pythonionecoding=utf-8'。
动机
- bash内置的msys和msys2的pwd具有特定于windows的-w选项,用于打印真正的windows路径。
- 但巴什的塞格温内置密码没有。
- -w选项当然是windows特有的,因此我们永远不能在*nix平台中使用它。
- cygwin、msys和msys2各自维护自己的挂载表,并且基于它们的绝对路径表达式彼此不同。
- If you are in these boxes, normally you want to follow these manners.
- But sometimes you need to know the path as Windows native path.
- If so, the lack of -W on cygwin’s pwd is the matter.
使用pwdpp作为python模块
pwdpp作为一个模块非常简单,它只公开两个方法和一个变量:
>>> import os >>> import pwdpp >>> pwdpp.__all__ [u'caller_type', u'curdir', u'main'] >>> pwdpp.caller_type (u'win32', u'mingw32', u'C:\\MinGW\\msys\\1.0\\bin\\sh.exe') >>> help(pwdpp.curdir) Help on function curdir in module pwdpp: curdir(physical, winpath=False) Return the current directory according to the following rules: +---------------+-----------------------------+----------------------+ | from | python | default | +===============+=============================+======================+ | cmd.exe | official CPython on windows | follow the behavior | | | | of windows native. | +---------------+-----------------------------+----------------------+ | msys | official CPython on windows | follow the behavior | | (bash, etc.) +-----------------------------+ of MSYS. | | | msys CPython? if any, | | +---------------+-----------------------------+----------------------+ | msys2 | official CPython on windows | follow the behavior | | (bash, etc.) +-----------------------------+ of MSYS2. | | | msys2 CPython | | +---------------+-----------------------------+----------------------+ | cygwin | official CPython on windows | follow the behavior | | (bash, etc.) +-----------------------------+ of cygwin. | | | cygwin CPython | | +---------------+-----------------------------+----------------------+ | *nix shell | various | follow the behavior | | | | of *nix native. | +---------------+-----------------------------+----------------------+ Except but the case following windows native, physical=True corresponds to "pwd -P", physical=False corresponds to "pwd -L". If winpath=True, this function follow the behavior of windows native, even if cygwin. Of course this makes no sense on real *nix. >>> pwdpp.curdir(True, True) u'c:\\Users\\hhsprings\\work' >>> pwdpp.curdir(True, False) u'/c/Users\\hhsprings\\work' >>> os.chdir("c:/MinGW/msys/1.0/bin") # my msys root >>> pwdpp.curdir(True, True) u'c:\\MinGW\\msys\\1.0\\bin' >>> pwdpp.curdir(True, False) u'\\usr\\bin' >>> pwdpp.curdir(True, False).replace(u"\\", u"/") u'/usr/bin'
main只是cli的入口点。
历史记录
1.0.6(2017-8-09)
- 源代码与1.0.3相同。(仅用于上传到pypi。)
1.0.4-1.05(2017-8-09)
- 不见了。上传到pypi是个错误。
1.0.3(2017-8-09)
- 修正cwd是否为msys根目录。
1.0.2(2017-8-09)
- 修复父级为“env.exe”的问题。
- 呼叫者类型的第二项已损坏…
1.0.1(2017-8-09)
- 修复Unicode问题(python 3.x)
1.0.0(2017-8-09)
- 首次发布