使用 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 ...
随机推荐
- C#冒泡排序法
基本原理 比较两个相邻的数的大小,每次比较完后把最大的数放到本轮的末尾.假设有数组: 258,445,131,97,22,36,17,38,28,28,第一轮:258和445比较,位置不用交换.第二轮 ...
- BuilderParttern(建造者模式)
/** * 建造者模式 * 主要用于构造复杂的对象 * 在优朋播放器就是采用建造者构建的,可以说比较有心得吧 * @author TMAC-J * */ public class BuilderPat ...
- 复杂的xml转化为java实体
一.样例一: 以根据订单号向支付宝查询支付是否成功为例(成功信息) 失败信息: <?xml version="1.0" encoding="utf-8"? ...
- 1-2 nodejs小节 文件读取
1.表达式 在命令行输入 node回车后,可以在后边输入相应的表达式,进行运算操作 2.阻塞文件读取 var data=fs.readFileSync('input.txt', 'utf-8') ...
- iOS多图片下载
iOS多图片下载.在cell里面下载图片.做了缓存优化. (app.icon是图片地址) // 先从内存缓存中取出图片 UIImage *image = self.images[app.icon]; ...
- User Get 'Access Denied' with Excel Service WebPart
用户可以访问网站,并且具有相应的访问权限. 用户尝试了其他浏览器和IE的其他版本. 解决: 将用户添加到 ‘Excel Services Views’ 这个SharePoint组就OK了.
- 用Kotlin开发Android应用(I):介绍
关于Kotlin,网上已有一些介绍的文章,包括Antonio Leiva的这组blog翻译稿.不过,我还是想跟进它们.翻译它们,以锻炼自己的英文翻译.各位高手发现问题,请及时“拍砖”. 原文题目:Ko ...
- [Android]一个干净的架构(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5276587.html 一个干净的架构 原文:https://b ...
- AES 加密工具类
/** * AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化: */public class AESOperator { /* * 加密用的 ...
- 【android 开 发 】 - Android studio 下 NDK Jni 开发 简单例子
Android 开发了一段时间,一方面 ,感觉不留下点什么.有点对不起自己, 另一方面,好记性不如烂笔头,为了往后可以回头来看看,就当做是笔记,便决定开始写博客.废话不多说 ! 今天想搞一搞 ndk ...