shell实用程序的集合

shelmet的Python项目详细描述


versionbuildcoverallslicense

shell实用程序的集合

特点

  • Shell实用程序,如…
    • cpmvmkdirtouch
    • rmrmfilermdir
    • lslsfileslsdirs
    • walkwalkfileswalkdirs
    • cdenviron
    • 还有更多!在
  • 100%测试覆盖率
  • 完全类型注释
  • Python 3.6+

快速启动

使用pip安装:

pip3 install shelmet

导入sh模块:

^{pr2}$

执行一些文件操作:

# Make directories and sub-directories. Behaves like "$ mkdir -p"sh.mkdir("a","b","c","d/e/f/g")# Context-manager to change working directory temporarily. Behaves like "$ cd".withsh.cd("d/e/f/g"):sh.touch("1.txt","2.txt","3.txt")# Move files or directories. Works across file-systems. Behaves like "$ mv".sh.mv("1.txt","11.txt")# Copy files or directories. Behaves like "$ cp -r"sh.cp("2.txt","22.txt")# List top-level directory contents.# NOTE: sh.ls() and its siblings are generators.list(sh.ls())# Limit to files.list(sh.lsfiles())# Limit to directories.list(sh.lsdirs())# Remove files.sh.rmfile("11.txt","22.txt","3.txt")# Or use sh.rm which handles both files and directories.sh.rm("11.txt","22.txt","3.txt")# Recursively walk current directory.# NOTE: sh.walk() and its siblings are generators.list(sh.walk())# Or just a specified directory.list(sh.walk("d"))# Or just it's files or directories.list(sh.walkfiles())list(sh.walkdirs())# Remove directories.sh.rmdir("a","b","c","d")# Or use sh.rm which handles both files and directories.sh.rm("a","b","c","d")

以原子方式写入新文件,其中内容将写入临时文件,完成后再移动:

importoswithsh.atomicfile("path/to/atomic.txt")asfp:# Writes are sent to a temporary file in the same directory as the destination.print(fp.name)# will be something like "path/to/.atomic.txt_XZKVqrlk.tmp"fp.write("some text")fp.write("some more text")# File doesn't exist yet.assertnotos.path.exists("path/to/atomic.txt")# Exiting context manager will result in the temporary file being atomically moved to destination.# This will also result in a lower-level fsync on the destination file and directory.assertos.path.exists("path/to/atomic.txt")# File mode, sync skipping, and overwrite flag can be specified to change the default behavior which is...withsh.atomicfile("file.txt","w",skip_sync=False,overwrite=True):pass# Additional parameters to open() can be passed as keyword arguments.withsh.atomicfile("file.txt","w",**open_kwargs):pass

以原子方式创建一个新目录,其中的内容将写入临时目录,然后在完成后移动:

withsh.atomicdir("path/to/atomic_dir")aspath:# Yielded path is temporary directory within the same parent directory as the destination.# path will be something like "path/to/.atomic_dir_QGLDfPwz_tmp"some_file=path/"file.txt"some_file.write_text("contents")# file written to "path/to/.atomic_dir_QGLDfPwz_tmp/file.txt"some_dir=path/"dir"some_dir.mkdir()# directory created at "path/to/.atomic_dir_QGLDfPwz_tmp/dir/"# Directory doesn't exist yet.assertnotos.path.exists("path/to/atomic_dir")# Exiting context manager will result in the temporary directory being atomically moved to destination.assertos.path.exists("path/to/atomic_dir")# Sync skipping and overwrite flag can be specified to change the default behavior which is...withsh.atomicdir("atomic_dir",skip_sync=False,overwrite=True):pass

临时更改环境变量:

# Extend existing environment.withsh.environ({"KEY1":"value1","KEY2":"value2"})asnew_environ:# Do something while environment changed.# Environment variables include all previous ones and {"KEY1": "value1", "KEY2": "value2"}.pass# Replace the entire environment with a new one.withsh.environ({"KEY":"value"},replace=True):# Environment variables are replaced and are now just {"KEY": "value"}.pass

有关详细信息,请参阅位于https://shelmet.readthedocs.io的完整文档。在

变更日志

版本0.2.0(2020-11-30)

  • 添加到sh模块:
    • atomicdir
  • atomic_write重命名为atomicfile中断更改

版本0.1.0(2020-11-16)

  • 第一次发布。在
  • 添加sh模块:
    • atomic_write
    • cd
    • cp
    • dirsync
    • environ
    • fsync
    • getdirsize
    • ls
    • lsdirs
    • lsfiles
    • mkdir
    • mv
    • reljoin
    • rm
    • rmdir
    • rmfile
    • touch
    • umask
    • walk
    • walkdirs
    • walkfiles

麻省理工学院执照

版权所有(c)2020 Derrick Gilland

特此免费授予获得本软件和相关文档文件(以下简称“软件”)副本的任何人无限制地使用本软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向其提供软件的人提供软件,但须符合以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。在

本软件按“原样”提供,无任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性的保证。在任何情况下,作者或版权持有人均不对因本软件或本软件的使用或其他交易而引起的、与之相关的任何索赔、损害赔偿或其他责任负责,无论是合同诉讼、侵权诉讼还是其他诉讼。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用JNA创建同一库的多个实例?   java在将Graphql查询作为JSON字符串传递时收到意外的令牌错误   OAuth2 oltu的java问题   java桌面应用程序使用的好的嵌入式数据库是什么?   java Firebase数据库高级查询选项   java正在使磁盘上的EhCache元素过期   java 安卓还原处于backstack中的片段的实例状态   XMemcached中的java异步集   java TimescaleDB是否使用与Postgresql完全相同的JDBC驱动程序?   java从网站c读取信息#   检查java Android中的字符串是否只包含数字和空格   c#如何向web服务发送特殊字符?   grails无法调用需要java的方法。lang.类参数?   java我在组合框中调用的方法不会运行所有代码,它只运行部分代码   java发送带有标头的HTTP GET请求