第20个python程序:函数和文件
来源:互联网 发布:淘宝打印电子面单 编辑:程序博客网 时间:2024/05/29 08:25
[root@mysql1 pshell]# cat ex20.py
#!/usr/bin/env Python
#-*-coding:utf-8-*-
from sys import argv
script,input_file=argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0)
def print_a_line(line_count,f):
print line_count,f.readline()
current_file=open(input_file)
print "first let's print the whole file:\n"
print_all(current_file)
print "now let's rewind,kind of like a tape."
rewind(current_file)
print "let's print three lines:"
current_line=1
print_a_line(current_line,current_file)
current_line=current_line+1
print_a_line(current_line,current_file)
current_line=current_line+1
print_a_line(current_line,current_file)
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]# python ex20.py test.txt
first let's print the whole file:
111111
22222222
3333333333
aaaaaa
bbbbbbbb
ccccccccccc
now let's rewind,kind of like a tape.
let's print three lines:
1 111111
2 22222222
3 3333333333
#!/usr/bin/env Python
#-*-coding:utf-8-*-
from sys import argv
script,input_file=argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0)
def print_a_line(line_count,f):
print line_count,f.readline()
current_file=open(input_file)
print "first let's print the whole file:\n"
print_all(current_file)
print "now let's rewind,kind of like a tape."
rewind(current_file)
print "let's print three lines:"
current_line=1
print_a_line(current_line,current_file)
current_line=current_line+1
print_a_line(current_line,current_file)
current_line=current_line+1
print_a_line(current_line,current_file)
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]# python ex20.py test.txt
first let's print the whole file:
111111
22222222
3333333333
aaaaaa
bbbbbbbb
ccccccccccc
now let's rewind,kind of like a tape.
let's print three lines:
1 111111
2 22222222
3 3333333333
0 0
- 第20个python程序:函数和文件
- 第6个python程序:字符串和文本
- Learn Python The Hard Way学习(20) - 函数和文件
- 笨方法学Python 习题 20: 函数和文件
- 笨方法学习Python-习题20: 函数和文件
- 笨办法学python习题20 函数和文件
- Exercise 20: 函数和文件
- 习题20 函数和文件
- exercise 20 函数和文件
- python调用函数、类和文件总结
- 练习20——函数和文件
- 笨办法20函数和文件
- python20 函数和文件
- 函数和文件处理
- python基础(3) 匿名函数和文件处理、异常
- 笨方法学python: ex20, 函数和文件
- Python的函数、模块和文件I/O
- python遍历文件夹和文件
- 一位资深程序员大牛给予Java初学者的学习路线建议
- 驾校补考缴费
- elasticsearch Getting Started (二)-安装
- JUnit简单使用教程
- 【项目管理和构建】——Maven下载、安装和配置
- 第20个python程序:函数和文件
- webstorm文件类型关联错误 导致文件打开方式不对 修改文件关联设置
- 【费用流】[CodeForces - 717G]Underfail
- CodeSmith Generator 7.0.2激活步骤
- 常用扩展方法总结
- SharedPreference的简单使用
- startActivityForResult用法详解(一)
- Ext自适应布局(fit)
- c#读写App.config,ConfigurationManager.AppSettings