设为首页 加入收藏

TOP

使用Python实现Linux系统wc命令(一)
2017-06-04 10:22:33 】 浏览:361
Tags:使用 Python 实现 Linux 系统 命令

使用Python实现Linux系统wc命令,效果一样。


代码如下:
#!/usr/bin/python
#*-*coding:utf8*-*
 
import sys
import os
from optparse import OptionParser
 
"""定义参数"""
parser = OptionParser()
parser.add_option("-l", "--line",
                  dest="lines",
                  action="store_true",
                  default=False,
                  help="only count lines")
parser.add_option("-w", "--word",
                  dest="words",
                  action="store_true",
                  default=False,
                  help="only count words")
parser.add_option("-c", "--char",
                  dest="chars",
                  action="store_true",
                  default=False,
                  help="only count chars")
parser.add_option("-n", "--nototal",
                  dest="nototal",
                  action="store_true",
                  default=False,
                  help="no count total")
options, args = parser.parse_args()
 
"""根据指定不同选项显示不同的值"""
def display(l, w, c):
    global total_l
    total_l += l
    global total_w
    total_w += w
    global total_c
    total_c += c
    if not (options.words or options.chars or options.lines):
        print(l),
        print(w),
        print(c),
    if options.lines:
        print(l),
    if options.words:
        print(w),
    if options.chars:
        print(c),
 
"""针对文件特殊处理,如果是1个文件以上那么需要输入一个Total总和"""
def dir(data):
    if not os.path.exists(data):
        sys.stderr.write("%s No such file or directory\n" %data)
        return False
    if os.path.isdir(data):
        sys.stderr.write("%s Is a directory\n" %data)
        return False
    return True
         
def readFile(data):
    for f in data:
        b = dir(f)
        if b:
            with open(f) as files:
                fd = files.read()
                l = fd.count("\n")
    &nbs

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇使用Python统计Linux系统内存 下一篇Python实现猜数字游戏

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目