简明python教程十----python标准库
import sys def readfile(filename):
'Print a file to the standard output.'
f=file(filename)
while True:
line=f.readline()
if len(line)==:
break
print line,
f.close() if len(sys.argv) <:
print 'No action specified.'
sys.exit() if sys.argv[].startswith('--'):
option = sys.argv[][:]
if option == 'version':
print 'Version 1.2'
elif option =='help':
print'''\
This program prints files to the standard output.
Any number of files can be specified.
Option include:
--version:Prints the version number
--help:Display this help'''
else:
for filename in sys.argv[:]:
readfile(filename)
结果:
$ python cat.py
No action specified. $ python cat.py --help
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help $ python cat.py --version
Version 1.2 $ python cat.py --nonsense
Unknown option. $ python cat.py poem.txt
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
在python程序运行的时候,即不是在交互模式下,在sys.argv列表中总是至少有一个项目。它就是当前运行的程序名称,作为sys.argv[0]。
sys模块
>>>import sys
>>> sys.version
sys.version字符串给你提供安装的Python的版本信息。sys.version_info元组则提供一个更简单的方法来使你的程序具备python版本要求功能。
sys.stdin、sys.stdout、sys.stderr它们分别对应你的程序的标准输入、标准输出和标准错误流。
OS模块
这个模块包含普通的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。
即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在linux和widows下运行。
os.sep:可以取代操作系统特定的路径分隔符。
os.name字符串指示你正在使用的平台。比如windows是‘nt’,linux/Unix用户,它是‘posix’
os.getcwd()函数:得到当前工作目录,即当前python脚本工作的目录路径。
os.getenv()和os.putenv()函数:读取和设置环境变量。
os.listdir():返回指定目录下的所有文件和目录名
os.remove()函数:删除一个文件
os.system()函数:运行shell命令
os.linesep字符串给出当前平台使用的行终止符。windows使用‘\r\n’,linux使用‘\n’,而Mac使用‘\r’。
os.path.split()函数:返回一个路径的目录名和文件名
os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。
os.path.exists()函数:检验给出的路径是否真正存在。
简明python教程十----python标准库的更多相关文章
- python代码规范与标准库参考
python代码规范与标准库参考 python代码规范参考文献: http://www.runoob.com/w3cnote/google-python-styleguide.html https:/ ...
- python入门灵魂5问--python学习路线,python教程,python学哪些,python怎么学,python学到什么程度
一.python入门简介 对于刚接触python编程或者想学习python自动化的人来说,基本都会有以下python入门灵魂5问--python学习路线,python教程,python学哪些,pyth ...
- A Byte of Python(简明Python教程) for Python 3.0 下载
A Byte of Python v1.92 (for Python 3.0) 官方下载地址,当前(20120730) 最新版本 1.92 基于Python3的 下载: http://files.s ...
- 【循序渐进学Python】11.常用标准库
安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间.这里是一些常用标准库的简单说明.更多的标准库的说明,可以参考Python文档 sys 模块 ...
- 介绍下Python的两个标准库 os 和 sys
import sysprint(sys.path) #python 2 中报错 ....,打印的是绝对路径(***\\python\\lib\\site-packages# 第三方库,后退一级为标准库 ...
- 如何美观地打印 Python 对象?这个标准库可以简单实现
前不久,我写了一篇文章回顾 Python 中 print 的发展历史 ,提到了两条发展线索: 明线:早期的 print 语句带有 C 和 Shell 的影子,是个应用程序级的 statement,在最 ...
- python:模块1——标准库简介
一.文档 windows系统:IDLE中打开帮助文档 Tutorial:简单入门 Library Reference:python内置函数和标准库(看不完的,当做字典来查)(此外还有pypi(拍派社区 ...
- Python模块进阶、标准库、扩展库
模块进阶 Python有一套很有用的标准库(standard library).标准库会随着Python解释器,一起安装在你的电脑中的. 它是Python的一个组成部分.这些标准库是Python为你准 ...
- python中时间处理标准库DateTime加强版库:pendulum
DateTime 的时区问题 Python的datetime可以处理2种类型的时间,分别为offset-naive和offset-aware.前者是指没有包含时区信息的时间,后者是指包含时区信息的时间 ...
随机推荐
- alert的美化,并且随滚动条滚动
onclick="sAlert('${vo.courseName}');" <script type="text/javascript" language ...
- 2015 Multi-University Training Contest 5 1009 MZL's Border
MZL's Border Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5351 Mean: 给出一个类似斐波那契数列的字符串序列 ...
- 【BZOJ】1034: [ZJOI2008]泡泡堂BNB(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1034 弱的比弱的强就用,强的比强的强就用: 否则弱的和强的比. 输的情况就是2n-ans(b,a), ...
- 剑指 offer set 23 n 个骰子的点数
题目 把 n 个骰子扔到地上, 所有骰子朝上一面的点数之和为 s. 输入 n, 打印出 s 所有可能的值出现的概率. 思路 1. 基于递归的求解方法. 深度为 n 的 dfs, 相当于求全排列, 时间 ...
- iOS实现截屏 并合适保存
本文转载至:http://blog.csdn.net/zeng11088/article/details/8664510 分类: UIImageView2013-03-12 16:42 122人阅读 ...
- spring基础---->spring自定义标签(一)
Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...
- FluentNhibernate 不支持存储过程
一直以为没有使用FN进行存储过程的操作,这次因为后台首页想统计下数据,就利用了存储过程,但在使用中却发现FN目前还不支持存储过程(点击查看官方),没有办法,只能利用Fluent Configurati ...
- 《从零开始学Swift》学习笔记(Day 64)——Cocoa Touch设计模式及应用之目标与动作
原创文章,欢迎转载.转载请注明:关东升的博客 目标(Target)与动作(Action)是iOS和OS X应用开发的中事件处理机制. 问题提出 如图所示是一个ButtonLabelSample案例 ...
- spring配置文件注解方式引入的两种方式
一.#{beanID['propertiesName']}方式 <bean id="propertyConfigurer" class="org.springfra ...
- python--get_data_from_csv_or_txt
一.从csv文件中获取 import osimport csv class GetDataFromCsvFile(): def __init__(self, csv_file, params_list ...