关于Windows系统下,Python读取文件的问题。
来源:互联网 发布:香港有淘宝吗 编辑:程序博客网 时间:2024/05/07 04:45
在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!
- 关于Windows系统下,Python读取文件的问题。
- 关于getprivateprofilestring 读取 windows 文件夹下ini文件异常问题的探究
- 关于Python读取文件的路径中斜杠问题
- linux下读取windows文件遇到的问题
- 关于windows下虚拟机系统与真实机文件共享的问题 ---基于VMware Workstation
- Python读取绝对路径下的文件的问题
- Windows系统关于Mysql-Python无法安装的问题
- Windows环境下Python代码的文件路径问题
- 关于Windows下python编码问题
- 关于python读取大文件的方法
- 关于python文件的读取创建写入
- python关于CSV文件的读取
- 关于Python读取中文(可能也包括其他一些奇葩的字符)文件,并成功在Windows终端上显示的问题
- 解决Windows下Python程序读取中文文件
- windows环境下python安装open cv 读取tiff文件
- Python读取文件夹下的所有文件
- Python读取路径下的所有文件
- python读取文件下的所有图像
- Linux中搭建SVN服务器
- C++ 堆排序算法
- 组合数 南阳oj32【DFS】
- 理解RESTful架构
- combox
- 关于Windows系统下,Python读取文件的问题。
- 看到的java问题解析
- 设计模式之spring分析
- 当前时间的三天
- 练字心得
- 4.6向左或向右 ---RadioGroup组与onCheckedChanged事件
- SQL删除所有重复数据。
- IOS 实现自定义的导航栏背景以及自定义颜色的状态栏
- Count Complete Tree Nodes