zoukankan      html  css  js  c++  java
  • Python3.2官方文档翻译--标准库概览(一)

    7.1 操作系统接口

    Os模块提供主要很多与操作系统交互的函数。

     

    >>> import os

    >>> os.getcwd() # Return the current working directory

    C:\Python31

    >>> os.chdir(/server/accesslogs) # Change current working directory

    >>> os.system(mkdir today) # Run the command mkdir in the system shell

    0

    一定要用import os 方式取代 from os import *. 这会使os.open()方法覆盖内置的open()函数。

    由于它们操作有非常大的不同。

    内置方法dir()help()方法对交互的使用像os这样的大模块很实用。

    >>> import os

    >>> dir(os)

    <returns a list of all module functions>

    >>> help(os)

    <returns an extensive manual page created from the modules docstrings>

    对于日用文件和文件夹管理任务,shutill模块提供一个更高级别的并方便使用的接口。

    >>> import shutil

    >>> shutil.copyfile(data.dbarchive.db)

    >>> shutil.move(/build/executablesinstalldir)

    7.2 文件通配符

    Glob模块提供一个函数用来从文件夹通配符搜索中生产文件列表。

    >>> import glob

    >>> glob.glob(*.py)

    [primes.pyrandom.pyquote.py]

    7.3 命令行參数

    共同的工具脚本经常须要提供命令行參数。这些參数作为列表保存在sys模块中argv属性中。比如。接下来输出通过在命令行执行python demo.py one two three 得到的结果。

    >>> import sys

    >>> print(sys.argv)

    [demo.pyonetwothree]

    Getopt模块用Unix的习惯getopt()函数来执行sys.argv. argparse模块提供了很多更加作用强大和灵活的命令行操作。

    7.4 错误输出重定向和程序终止

    Sys模块还包含很多属性如 stdinstdoutstderr。后面的属性通经常使用来抛出警告或者错误信息,当stdout重定向时候也能够看到错误信息。

    终止脚本的最直接方法就是用sys.exit()方法。

    >>> sys.stderr.write(Warning, log file not found starting a new one )

    Warning, log file not found starting a new one

  • 相关阅读:
    C#模拟百度登录并到指定网站评论回帖(一)
    4张图看明白用户、权限和租户的关系
    我的微服务之路
    IT部门不应该是一个后勤部门
    一个值只有0和1的字段,到底要不要建索引?
    论程序员的自我修养
    RBAC权限管理系统数据模型
    有史以来功能最全,使用最简单的excel导入/导出工具
    一个完全平均分布的固定长度随机数发生器
    基于WCF的RESTFul WebAPI如何对传输内容实现压缩
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6708409.html
Copyright © 2011-2022 走看看