这里使用了 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 命令程序的基本功能的更多相关文章

  1. Python模拟wc命令(软件测试第二次作业)

    Python实现字符,单词,行,代码行,空行及可视化 Gitee项目地址:https://gitee.com/biubiubiuLYQ/word_and_character_statistics 一. ...

  2. 逐步实现python版wc命令

    Python 如何处理管道输入输出 sys.stdin 等于打开了一个文件对象,所有输入的文件都会写入到标准输入文件中(键盘) sys.stdout 等于打来了一个文件对象,使用.write()把信息 ...

  3. 作业(二)—python实现wc命令

    Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github) 0x00 前言 好久没发博客了,感觉自己的学习是有点偷懒了.这篇博客也是应专业 ...

  4. python学习:简单的wc命令实现

    #!/usr/bin/python   import sys import os   try:     fn = sys.argv[1] except IndexError:     print &q ...

  5. python 怎么启动一个外部命令程序, 并且不阻塞当前进程

    http://www.myexception.cn/perl-python/1278887.html http://blog.chinaunix.net/uid-25979788-id-3081912 ...

  6. 通过python实现wc基本功能

    ---恢复内容开始--- 1.Github项目地址: https://github.com/zhg1998/ww/blob/master/wc.py 2.项目相关要求: 写一个命令行程序,模仿已有wc ...

  7. grep 和 wc命令 --- !管道命令!

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...

  8. Python系统调用——运行其他程序

    转载:http://blog.csdn.net/ssihc0/article/details/7738527 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其 ...

  9. [python]用profile协助程序性能优化

    转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...

随机推荐

  1. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  2. Js运动框架

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  3. ASP.NET多种不同页面间数据传递的方法

    1. Get(即使用QueryString显式传递)     方式:在url后面跟参数.     特点:简单.方便.     缺点:字符串长度最长为255个字符:数据泄漏在url中.     适用数据 ...

  4. LINUX最大线程数及最大进程数

    查看最大线程数: cat /proc/sys/kernel/threads-max ulimit User limits - limit the use of system-wide resource ...

  5. Redis学习笔记——初级

    1. Redis是什么.特点.优势 Redis是一个开源的使用C语言编写.开源.支持网络.可基于内存亦可持久化的日志型.高性能的Key-Value数据库,并提供多种语言的API. 它通常被称为数据结构 ...

  6. ListView的CheckBox实现全部选中/不选中

    在Adapter类中定义一个HashMap列表,保存每一行是否被选中: private static HashMap<Integer, Boolean> isSelected; 可见定义了 ...

  7. Verilog HDL模型的不同抽象级别

    所谓不同的抽象类别,实际上是指同一个物理电路,可以在不同层次上用Verilog语言来描述.如果只从行为功能的角度来描述某一电路模块,就称作行为模块.如果从电路结构的角度来描述该电路模块,就称作结构模块 ...

  8. MySQL 5.7:非结构化数据存储的新选择

    本文转载自:http://www.innomysql.net/article/23959.html (只作转载, 不代表本站和博主同意文中观点或证实文中信息) 工作10余年,没有一个版本能像MySQL ...

  9. [转载]T-SQL(Oracle)语句查询执行顺序

    原文链接:http://blog.sina.com.cn/s/blog_61c006ea0100mlgq.html sql语法的分析是从右到左,where子句中的条件书写顺序,基本上对sql性能没有影 ...

  10. 怎么才能算大项目(Application),大的衡量?

    最近在思考一个问题,就是怎么才算一个大型项目,大的衡量有哪些东西?为什么要搞清这个问题呢,是因为在实际的开发过程中,如果知道这是个多大的项目,那就有各种相应的解决方案(我们称之为套路的东西)就可以使用 ...