- 浏览: 269809 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
Xujian0000abcd:
说的太好啦~赞一个~
shell if语句中的并列 -
Jimmy.song:
终于在楼主这里找到答案,很受益,谢谢~
使用diff或者vimdiff比较远程文件(夹)与本地文件(夹)
1.sys模块(未在windows下调试)
import sys
def readfile(filename):
'''Print a file to the standard output.'''
f = file(filename)
while True:
line = f.readline()
if len(line) == 0:
break
print line, # notice comma
f.close()
# Script starts from here
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
# fetch sys.argv[1] but without the first two characters
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.
Options include:
--version : Prints the version number
--help : Display this help'''
else:
print 'Unknown option.'
sys.exit()
else:
for filename in sys.argv[1:]:
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!
2.名称cat是concatenate的缩写,它可以打印一个文件或者把两个或两个以上文件连接/级连在一起打印。sys.version字符串给你提供安装的Python的版本信息sys.version_info元组则提供一个更简单的方法来使你的程序具备Python版本要求功能。
sys模块中其他令人感兴趣的项目有sys.stdin、sys.stdout和sys.stderr它们分别对应你的程序的标准输入、标准输出和标准错误流。
3.OS模块:使用这个模块可以使程序与平台无关
用法举例:
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.split('/home/swaroop/byte/code/poem.txt')
('/home/swaroop/byte/code', 'poem.txt')
os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。类似地,os.path.existe()函数用来检验给出的路径是否真地存在。
import sys
def readfile(filename):
'''Print a file to the standard output.'''
f = file(filename)
while True:
line = f.readline()
if len(line) == 0:
break
print line, # notice comma
f.close()
# Script starts from here
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
# fetch sys.argv[1] but without the first two characters
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.
Options include:
--version : Prints the version number
--help : Display this help'''
else:
print 'Unknown option.'
sys.exit()
else:
for filename in sys.argv[1:]:
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!
2.名称cat是concatenate的缩写,它可以打印一个文件或者把两个或两个以上文件连接/级连在一起打印。sys.version字符串给你提供安装的Python的版本信息sys.version_info元组则提供一个更简单的方法来使你的程序具备Python版本要求功能。
sys模块中其他令人感兴趣的项目有sys.stdin、sys.stdout和sys.stderr它们分别对应你的程序的标准输入、标准输出和标准错误流。
3.OS模块:使用这个模块可以使程序与平台无关
用法举例:
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.split('/home/swaroop/byte/code/poem.txt')
('/home/swaroop/byte/code', 'poem.txt')
os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。类似地,os.path.existe()函数用来检验给出的路径是否真地存在。
发表评论
-
pickle 模块
2012-11-15 16:44 929持久性就是指保持对象,甚至在多次执行同一程序之间也保持 ... -
python copy and deepcopy
2012-11-15 14:51 1619>>> a=[[1,2],(3,5),123 ... -
python 常用模块
2012-11-15 10:19 1375python除了关键字(keywords)和内置的类型和函数( ... -
字符串变成变量名
2012-11-02 11:47 3863使用字符串指代变量名。 比如说,有两个变量 a=" ... -
PYTHON--常用函数(二)
2012-08-30 16:33 1016类型转换函数 chr(i) chr()函数返回ASCII码对 ... -
PYTHON--常用函数(三)
2012-08-30 16:33 1311eval( expression[, globals[, lo ... -
PYTHON--常用函数(一)
2012-08-31 09:47 2249字符串常用函数 replace( ... -
DeprecationWarning: the sets module is deprecated from sets import Immut
2012-08-21 15:53 2114解决方法: 1) file "__init__&qu ... -
MySQLdb for Python 安装 windows
2012-08-21 15:31 25041、由于自己使用的是MySQL 5.5社区服务器版本,由于Wi ... -
python lambda
2011-10-19 16:13 3468Python支持一种有趣的语法,它允许你快速定义单行的最小函数 ... -
python
2011-08-29 10:42 1024在python中, def名可以作为参数在def中传递,在使用 ... -
Python中的Glob模块
2011-05-30 10:49 914glob模块是最简单的模块之一,内容非常少。用它可以查找符合特 ... -
urllib模块
2011-05-23 14:51 937urllib模块提供的上层接口,使我们可以像读取本地文件一样读 ... -
xml.sax.saxutils
2011-05-23 14:30 1638xml.sax.saxutils模块里面包含了很多在使用SAX ... -
Python ConfigParser模块的使用
2011-05-23 10:27 1123在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配 ... -
Python方法参数中的 * 和 **
2011-05-06 11:25 1548*args(实际上,*号后面跟着变量名)语法在Python中表 ... -
Python 字符串
2010-11-04 16:17 7361.join()方法"X".join(ar ... -
Python 正则表达式二
2010-11-04 15:52 1266一、字符串 1.python字符串通常有单引号('...')、 ... -
Python 正则表达式一
2010-11-04 14:00 13311.元字符:. ^ $ * + ? { [ ] \ | ( ) ... -
Python中OptionParser模块
2010-11-03 10:46 9358自己理解: 步骤:(1) OptionParser 构造参数 ...
相关推荐
主要介绍了python标准库sys和OS的函数使用方法与实例详解,需要的朋友可以参考下
python标准库.pdf python标准库.pdf python标准库.pdf python标准库.pdf python标准库.pdf python标准库.pdf python标准库.pdf python标准库.pdf
Python标准库3.7 Python标准库3.7 Python标准库3.7 Python标准库3.7
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别 os._exit() 和 sys.exit() os._exit() vs sys.exit() 概述 Python的程序有两中退出方式:os._exit(), sys.exit()。本文介绍这两种方式的...
Python3标准库中文PDF版本介绍 1.掌握python核心模块 2.全面详细介绍python内置函数 3.掌握python标准库中所有的工具类,方法的使用提升代码的健壮性,安全性,易读性。 4.本手册由python开发作者倾力提供。
python是一门高级脚本语言,本书介绍了Python常用库函数应用
python标准库 中文
Python标准库参考.pdf
Python标准库 参考手册 Python标准库3.6 参考手册 中文完整pdf版-CSDN下载
python标准库
Python标准库3.6 Python标准库3.6 Python标准库3.6 PDF
Python标准库中文版
资源分类:Python库 所属语言:Python 资源全名:systest-2.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python标准库参考.pdf
2014年辛星python标准库整理第一版,可能不全,求谅解,求支持。
这篇文章主要介绍了Python常用模块sys,os,time,random功能与用法,结合实例形式分析了Python模块sys,os,time,random功能、原理、相关模块函数、使用技巧与操作注意事项,需要的朋友可以参考下 本文实例讲述了Python...