这里使用了 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. webmagic的设计机制及原理-如何开发一个Java爬虫

    之前就有网友在博客里留言,觉得webmagic的实现比较有意思,想要借此研究一下爬虫.最近终于集中精力,花了三天时间,终于写完了这篇文章.之前垂直爬虫写了一年多,webmagic框架写了一个多月,这方 ...

  2. 1.Struts2简介和Struts2开发环境搭建

    一.Struts2简介: 1.什么是Struts2? 著名的SSH三大框架分别为:表现层(Struts).业务逻辑层(Spring),持久化层(Hibernate). Struts2是在WebWork ...

  3. 4年,如何从草根成长成为CTO-(第一篇)

    茫然的求索 那一年,刚好经济危机,毕业了.经过了很长时间的“网上海投”而杳无音讯之后,终于发现“经济危机真的和自己有点关系了” ,曾经以为经济危机和自己巴刚子打不着. 拿着简历,开始到处去跑招聘市场, ...

  4. UML类图简单介绍

    先上一张图: 概述 类图(Class Diagram)是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 类图组 ...

  5. Revit读取当前rvt的所有视图与其名称

    1)读取所有视图: public static ViewSet GetAllViews(Document doc) { ViewSet views = new ViewSet(); FilteredE ...

  6. 绿色简单的学校登录html页面

    效果预览:http://hovertree.com/texiao/css/22/ 代码如下: <!DOCTYPE html> <html> <head> <m ...

  7. Java Enumeration接口

    Enumeration接口定义 Enumeration接口与Iterator接口用法比较 一. 1.Enumeration接口定义 public interface Enumeration<E& ...

  8. keleyi菜单0.1.5版本发布了

    keleyi菜单是一个让你轻松创建向上弹出菜单的jquery插件. 最新版本0.1.5增加了显示三角形的功能,当一级菜单包含有子菜单时,会在一级菜单的右侧显示一个小三角形.如图所示: 查看例子:htt ...

  9. 很漂亮的用户登录界面HTML模板

    效果预览:http://keleyi.com/keleyi/phtml/divcss/21.htm HoverTree开源项目实现了分层后,准备实现管理员后台登录,这里先把登录界面的HTML模板整理好 ...

  10. jQuery静态方法noConflict的使用和源码分析

    所谓静态方法是jQuery本身得公共方法,并不需要通过实例化来调用,一般也称为工具方法,下面先来列绝下jQuery.noConflict方法的用法: noConflict() 方法让渡变量 $ 的 j ...