使用 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 ...
随机推荐
- OpenCV2邻域和模板操作
在图像处理中,通过当前位置的邻域像素计算新的像素值是很常见的操作.当邻域包含图像的上几行和下几行时,就需要同时扫描图像的若干行,这就是图像的邻域操作了.至于模板操作是实现空间滤波的基础,通常是使用一个 ...
- Windows Phone 的 TextBox 的实现 PropertyChanged
比如,View 的文本框 TextBox1 绑定了 ViewModel 的 Msg 属性, 当想把文本框输入的内容输入过程中实时更新到绑定的 Msg ,在Windows Phone 中是无法通过设置 ...
- CSS-清除浮动
什么是CSS清除浮动? 在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高 ...
- vue+sass 下sass不能运行问题
好久没写博文了,今天抽空写一写,最近在用vue.js build 项目,今早想使用sass来编译css,可是安装好依赖包之后仍然显示一下错误: ERROR in ENOENT: no such fil ...
- 迷惑很久,仅以个人想法谈谈MVC架构,希望大家多给点意见
博主是非科班出身,所以和大部分新手有着一样的困惑,究竟什么才能算是MVC框架,总是在谈Model,View,Controller分离,可是究竟什么才能算是分离,而他们又是负责什么样的分工呢. 大二的时 ...
- 网页中多个图标在一张图片上,使用css将各图标显示
现在的网页中显示很多图标算是常态,发现项目中页面上用到的图标都是单个图标单个文件,用的时候直接往页面上挂,这确实很常态. 如果,网站是挂在外网上,或者网速过低,又大量使用图标的情况下,由于浏览器和服务 ...
- case break结构与return的有关要点
//确认事件 private void cmd_ok_Click(object sender, EventArgs e) { //客户名称是否为空 if (txt_banhao.Text.TrimEn ...
- 基于CkEditor实现.net在线开发之路(3)常用From表单控件介绍与说明
上一章已经简单介绍了CKEditor控件可以编写C#代码,然后可以通过ajax去调用,但是要在网页上面编写所有C#后台逻辑,肯定痛苦死了,不说实现复杂的逻辑,就算实现一个简单增删改查,都会让人头痛欲裂 ...
- java timer 执行任务
1. 建立timer import java.util.Timer; import java.util.TimerTask; public class Start { public class Sta ...
- eclipse控制台下实现jdbc简单的增删改查测试
1.现在MySQL中创建一个表 2.首先创建一个类 //导入的包 import java.sql.Connection;import java.sql.DriverManager;import jav ...