使用 python 实现 wc 命令程序的基本功能
这里使用了 python 的基本代码实现了 Linux 系统下 wc 命令程序的基本功能。
#!/usr/bin/env python
#encoding: utf-8
# Author: liwei
# Function: wc program by python from optparse import OptionParser
import sys,os def opt():
parser = OptionParser()
parser.add_option('-c', '--char',
dest='chars',
action='store_true',
default=False,
help='only count chars') parser.add_option('-w', '--word',
dest='words',
action='store_true',
default=False,
help='only count words') parser.add_option('-l', '--line',
dest='lines',
action='store_true',
default=False,
help='only count lines') parser.add_option('-n', '--nototal',
dest='nototal',
action='store_true',
default=False,
help='don\'t print total information') options, args = parser.parse_args()
return options, args #print options
def get_count(data):
chars = len(data)
words = len(data.split())
lines = data.count('\n')
return lines, words, chars #if not options.chars and not options.words and not options def print_wc(options, lines, words, chars, fn):
if options.lines:
print lines,
if options.words:
print words,
if options.chars:
print chars,
print fn def main():
options, args = opt() if not (options.lines or options.words or options.chars):
options.lines, options.words, options.chars = True, True, True if args:
total_lines, total_words, total_chars = 0, 0, 0
for fn in args:
if os.path.isfile(fn):
with open(fn) as fd:
data = fd.read()
lines, words, chars = get_count(data)
print_wc(options, lines, words, chars, fn)
total_lines += lines
total_words += words
total_chars += chars
elif os.path.isdir(fn):
print >> sys.stderr, '%s is a directory' %fn
else:
sys.stderr.write('%s: No such file or directory\n' % fn)
# 只有多个文件的时候会计算出total字段
if len(args) > 1 and not options.nototal:
print_wc(options, total_lines, total_words, total_chars, 'total')
else:
fn = ''
data = sys.stdin.read()
lines, words, chars = get_count(data)
print_wc(options, lines, words, chars, fn) if __name__ == '__main__':
main()
使用 python 实现 wc 命令程序的基本功能的更多相关文章
- Python模拟wc命令(软件测试第二次作业)
Python实现字符,单词,行,代码行,空行及可视化 Gitee项目地址:https://gitee.com/biubiubiuLYQ/word_and_character_statistics 一. ...
- 逐步实现python版wc命令
Python 如何处理管道输入输出 sys.stdin 等于打开了一个文件对象,所有输入的文件都会写入到标准输入文件中(键盘) sys.stdout 等于打来了一个文件对象,使用.write()把信息 ...
- 作业(二)—python实现wc命令
Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github) 0x00 前言 好久没发博客了,感觉自己的学习是有点偷懒了.这篇博客也是应专业 ...
- python学习:简单的wc命令实现
#!/usr/bin/python import sys import os try: fn = sys.argv[1] except IndexError: print &q ...
- python 怎么启动一个外部命令程序, 并且不阻塞当前进程
http://www.myexception.cn/perl-python/1278887.html http://blog.chinaunix.net/uid-25979788-id-3081912 ...
- 通过python实现wc基本功能
---恢复内容开始--- 1.Github项目地址: https://github.com/zhg1998/ww/blob/master/wc.py 2.项目相关要求: 写一个命令行程序,模仿已有wc ...
- grep 和 wc命令 --- !管道命令!
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...
- Python系统调用——运行其他程序
转载:http://blog.csdn.net/ssihc0/article/details/7738527 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其 ...
- [python]用profile协助程序性能优化
转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...
随机推荐
- 1.Java网络编程之概述
黑马程序员_毕向东_Java基础视频教程第23天-01-网络编程(概述)学习笔记 网络通讯三要素: 1.IP地址 I.网络中设备的标识 II.不易记忆,可用主机名 www 万维网组织,baidu主机 ...
- RabbitMQ原理与相关操作(二)
接着 上篇随笔 增加几个概念: RabbitMQ是一个在AMQP(高级消息队列协议)标准基础上完整的,可服用的企业消息系统. AMQP模型的功能组件图(上图摘自 Sophia_tj 的 第2章 AMQ ...
- JS实现-页面数据无限加载
在手机端浏览网页时,经常使用一个功能,当我们浏览京东或者淘宝时,页面滑动到底部,我们看到数据自动加载到列表.之前并不知道这些功能是怎么实现的,于是自己在PC浏览器上模拟实现这样的功能.先看看浏览效果: ...
- BizTalk动手实验(十七)ODBC适配器使用
更多内容请查看:BizTalk动手实验系列目录 BizTalk 开发系列 1 课程简介 通过本课程熟悉ODBC适配器的的使用,本练习采用BizTalk 20 ...
- 初入网络系列笔记(4)HTTP请求和响应
一.借鉴说明,本博文借鉴以下博文 1.starok,HTTP必知必会,http://www.cnblogs.com/starstone/p/4890409.html 2.CareySon,HTTP协议 ...
- 用Kotlin改写PHP程序是什么样的体验
学Kotlin其实要看:http://kotlinlang.org/docs/kotlin-docs.pdf 在线版是不完整的!!!少了一些章节,会有点难看懂后面的文档. 我选择了WordPress里 ...
- PHP多维数组根据其中一个字段的值排序
平时简单的一维数组或者简单的数组排序这里就不多作介绍,这里主要是针对平时做项目中的可能遇到的情况,根据多维数组中的其中一个排序.用到的php函数是:array_multisort. 思路:获取其中你需 ...
- 圆形背景的TextView
[应用场景]: [需要的xml]:shape_circle.xml <?xml version="1.0" encoding="UTF-8"?>&l ...
- php 实现设计模式之 建造者模式
<?php /** * 建造者模式 * ------------- * 定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. * 类型:创建类模式 * 四个要素: * ...
- 关系型数据库与NOSQL
本文转载自: http://www.cnblogs.com/chay1227/archive/2013/03/17/2964020.html(只作转载, 不代表本站和博主同意文中观点或证实文中信息) ...