python学习:简单的wc命令实现
#!/usr/bin/python
import sys
import os
try:
fn = sys.argv[1]
except IndexError:
print "please follow a argument at %s" % __file__
sys.exit()
if not os.path.exists(fn):
print "%s is not exists" % fn
sys.exit()
fd = open(sys.argv[1])
data = fd.read()
chars = len(data)
words = len(data.split())
lines = data.count('\n')
print "%(lines)s %(words)s %(chars)s" % locals()
[root@web10 day1]# python 8_argv.py /etc/hosts
4 14 219
用 sys.argv 表示脚本的第一个参数 从文件读取实现wc命令