关于Windows系统下,Python读取文件的问题。
2015-08-10 10:56
621 查看
在windows系统下,Python读取文件需要输入整个路径的名称,可以不是C盘,也可以是中文名字的路径。
输入的方法是函数 open(文件地址,’r’)
‘r’ 表示读取,’w’ 表示写入,’a’ 表示添加,’+’ 表示读写,’b’ 表示二进制访问
测试代码如下:
a)
filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
fobj.close()
运行后
>>>
Enter file name:G:\interesting\text.txt
I am a learner,
I like Python,
I want Python read this file.
Hello World
b) filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
print 'You makeit! Congratulations!'
fobj.close()
运行后
Enter file name:G:\interesting\text.txt
I am a learner,
You make it! Congratulations!
You make it! Congratulations!
I like Python,
You make it! Congratulations!
You make it! Congratulations!
I want Python read this file.
You make it! Congratulations!
You make it! Congratulations!
Hello World You make it! Congratulations!
之所以这里会有两行’ You make it! Congratulations!’是因为我在编辑text.txt时候,每一行后面都输入了两个回车
c)
filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
print '\n\
\
You make it! Congratulations!'
fobj.close()
运行后
>>>
Enter file name:G:\interesting\text.txt
I am a learner,
I like Python,
I want Python read this file.
Hello World
You make it! Congratulations!
在这里,我们发现,在print函数当中,’\n’会打印一个回车,’\’表示换行后继续打印,但是不管换几行,都会直接跳过这些回车,而不是打印这些回车。
d)更改文件名为’测试.txt’
filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
print '\n\
\
You make it! Congratulations!'
fobj.close()
运行后
>>>
Enter file name:G:\interesting\测试.txt
I am a learner,
I like Python,
I want Python read this file.
Hello World
You make it! Congratulations!
输入的方法是函数 open(文件地址,’r’)
‘r’ 表示读取,’w’ 表示写入,’a’ 表示添加,’+’ 表示读写,’b’ 表示二进制访问
测试代码如下:
a)
filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
fobj.close()
运行后
>>>
Enter file name:G:\interesting\text.txt
I am a learner,
I like Python,
I want Python read this file.
Hello World
b) filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
print 'You makeit! Congratulations!'
fobj.close()
运行后
Enter file name:G:\interesting\text.txt
I am a learner,
You make it! Congratulations!
You make it! Congratulations!
I like Python,
You make it! Congratulations!
You make it! Congratulations!
I want Python read this file.
You make it! Congratulations!
You make it! Congratulations!
Hello World You make it! Congratulations!
之所以这里会有两行’ You make it! Congratulations!’是因为我在编辑text.txt时候,每一行后面都输入了两个回车
c)
filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
print '\n\
\
You make it! Congratulations!'
fobj.close()
运行后
>>>
Enter file name:G:\interesting\text.txt
I am a learner,
I like Python,
I want Python read this file.
Hello World
You make it! Congratulations!
在这里,我们发现,在print函数当中,’\n’会打印一个回车,’\’表示换行后继续打印,但是不管换几行,都会直接跳过这些回车,而不是打印这些回车。
d)更改文件名为’测试.txt’
filename = raw_input('Enter file name:')
fobj = open (filename, 'r')
for eachLine in fobj:
print eachLine,
print '\n\
\
You make it! Congratulations!'
fobj.close()
运行后
>>>
Enter file name:G:\interesting\测试.txt
I am a learner,
I like Python,
I want Python read this file.
Hello World
You make it! Congratulations!
相关文章推荐
- win32下利用python操作printer
- Python Paste.deploy 笔记
- python 清理mail spool 下的小脚本
- 递归
- 你也可以有自己的天气预报啦!
- 0.python基本算法
- Solaris 10u11 安装python2.7.10
- python--获取网页图片
- python列表排序
- Python之Window环境搭建
- Python于*args 和**kwargs使用
- 零基础学python-1.6 错误的程序
- 零基础学python-1.6 错误的程序
- 正则匹配不包含某字符串(Python)
- 使用python执行复制时遇到的一点问题
- [译] 12步轻松搞定python装饰器 - 简书
- python实现爬取千万淘宝商品的方法_python_脚本之家
- 值得关注的10个python语言博客 - WEB开发者
- python - ImportError: No module named pywintypes
- 详解Python中dict与set的使用