第15个python程序:读文件
2016-09-08 10:42
363 查看
[root@mysql1 pshell]# cat ex15.py
#!/usr/bin/env Python
#-*-coding:utf-8-*-
from sys import argv
script,filename = argv
txt = open(filename)
print "here's your file %r:" % filename
print txt.read()
print "type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
[root@mysql1 pshell]# cat ex15_sample.txt
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
[root@mysql1 pshell]#
[root@mysql1 pshell]# python ex15.py ex15_sample.txt
here's your file 'ex15_sample.txt':
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
type the filename again:
> ex15_sample.txt
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
[root@mysql1 pshell]# python ex15.py /root/pshell/ex15_sample.txt
here's your file '/root/pshell/ex15_sample.txt':
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
type the filename again:
> /root/pshell/ex15_sample.txt
Traceback (most recent call last):
File "ex15.py", line 14, in <module>
txt_again = open(file_again)
IOError: [Errno 2] No such file or directory: '/root/pshell/ex15_sample.txt ' ---------输入绝对文件名的时候后面多了个空格
[root@mysql1 pshell]# python ex15.py /root/pshell/ex15_sample.txt
here's your file '/root/pshell/ex15_sample.txt':
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
type the filename again:
> /root/pshell/ex15_sample.txt
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
#!/usr/bin/env Python
#-*-coding:utf-8-*-
from sys import argv
script,filename = argv
txt = open(filename)
print "here's your file %r:" % filename
print txt.read()
print "type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
[root@mysql1 pshell]# cat ex15_sample.txt
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
[root@mysql1 pshell]#
[root@mysql1 pshell]# python ex15.py ex15_sample.txt
here's your file 'ex15_sample.txt':
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
type the filename again:
> ex15_sample.txt
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
[root@mysql1 pshell]# python ex15.py /root/pshell/ex15_sample.txt
here's your file '/root/pshell/ex15_sample.txt':
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
type the filename again:
> /root/pshell/ex15_sample.txt
Traceback (most recent call last):
File "ex15.py", line 14, in <module>
txt_again = open(file_again)
IOError: [Errno 2] No such file or directory: '/root/pshell/ex15_sample.txt ' ---------输入绝对文件名的时候后面多了个空格
[root@mysql1 pshell]# python ex15.py /root/pshell/ex15_sample.txt
here's your file '/root/pshell/ex15_sample.txt':
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
type the filename again:
> /root/pshell/ex15_sample.txt
this is stuff i typed into a file.
it is really cool stuff.
lots and lots of fun to have in here.
相关文章推荐
- 在WINDOWS上将Python 文件转为(生成)可执行程序 exe 文件
- 一个删除文件的Python程序
- 我的第一个Python程序--批量生成Oracle DDL文件
- 用python写的测试网页和文件编码的小程序
- python程序:备份MS SQL数据库到压缩文件
- [Python]用Python编写程序修复"恶邮差"变种U病毒感染文件。
- 用python写的测试网页和文件编码的小程序
- Python程序:批理转化Excel成CSV文件
- python查看某一个文件/程序是否是可执行的
- 在WINDOWS上将Python 文件转为(生成)可执行程序 exe 文件
- python 提取文件的小程序
- py2exe打包python文件成可执行程序
- 用python实现一个按需生成用于vim跳转的tags文件的小程序
- 使用 cx_freeze 把python程序编译成可执行文件
- Python中删除文件的程序代码
- 将Python程序打包成可执行文件
- python写的统计PYTHON文件的(代码行数,空行数,注释行数)小程序
- 一段小程序,把某个文件从A路径按照某速度复制到B路径(python)
- python 合并文件小程序
- Python程序输出到文件中