Python文件操作类操作实例详解
作者:shichen2014 时间:2023-05-19 04:45:08
本文讲述了Python文件操作类的操作实例,详细代码如下:
#!/usr/bin/env python
#!/usr/bin/env python
#coding:utf-8
# Purpose: 文件操作类
#声明一个字符串文本
poem='''
Programming is fun测试
When the work is done
if you wanna make your work also fun:
use Python!
'''
#创建一个file类的实例,模式可以为:只读模式('r')、写模式('w')、追加模式('a')
f=file('poem.txt','a') #open for 'w'riting
f.write(poem) #写入文本到文件 write text to file
f.close() #关闭文件 close the file
#默认是只读模式
f=file('poem.txt')
# if no mode is specified,'r'ead mode is assumed by default
while True:
line=f.readline() #读取文件的每一个行
if len(line)==0: # Zero length indicates EOF
break
print line, #输出该行
#注意,因为从文件读到的内容已经以换行符结尾,所以我们在输出的语句上使用逗号来消除自动换行。
#Notice comma to avoid automatic newline added by Python
f.close() #close the file
#帮助
help(file)
标签:Python,文件,操作类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解JavaScript对象的深浅复制
2024-05-11 09:36:30
![](https://img.aspxhome.com/file/2023/1/125951_0s.png)
python读取excel指定列数据并写入到新的excel方法
2022-04-06 20:15:46
Golang实现异步上传文件支持进度条查询的方法
2024-05-08 10:13:17
基于Python OpenCV和 dlib实现眨眼检测
2021-05-28 18:50:28
![](https://img.aspxhome.com/file/2023/7/76847_0s.jpg)
CentOS 7.0如何启动多个MySQL实例教程(mysql-5.7.21)
2024-01-14 10:56:45
![](https://img.aspxhome.com/file/2023/1/76461_0s.png)
图片放大镜jquery.jqzoom.js使用实例附放大镜图标
2024-04-30 08:51:05
![](https://img.aspxhome.com/file/2023/0/132660_0s.gif)
Python 实现文件读写、坐标寻址、查找替换功能
2021-06-02 07:36:42
![](https://img.aspxhome.com/file/2023/4/127014_0s.jpg)
python机器学习之线性回归详解
2022-10-20 19:14:49
![](https://img.aspxhome.com/file/2023/0/120360_0s.png)
django redis的使用方法详解
2023-08-03 10:54:30
vue 获取视频时长的实例代码
2024-04-09 10:49:16
IDEA使用properties配置文件进行mysql数据库连接的教程图解
2024-01-22 13:45:08
![](https://img.aspxhome.com/file/2023/5/118205_0s.png)
部署.Net6项目到docker
2024-06-05 15:43:46
![](https://img.aspxhome.com/file/2023/5/122835_0s.jpg)
anaconda jupyter不能导入安装的lightgbm解决方案
2021-09-15 19:24:45
![](https://img.aspxhome.com/file/2023/5/83855_0s.jpg)
完美解决jupyter由于无法import新包的问题
2021-09-19 01:21:11
Python网络爬虫项目:内容提取器的定义
2021-05-29 21:51:43
![](https://img.aspxhome.com/file/2023/6/121036_0s.png)
python实现简单多人聊天室
2022-08-17 15:04:13
![](https://img.aspxhome.com/file/2023/7/67887_0s.jpg)
accept-charset与Header P3P
2009-04-01 18:43:00
一文速学Python+Pyecharts绘制树形图
2023-07-28 12:05:27
![](https://img.aspxhome.com/file/2023/1/61201_0s.png)
python设置环境变量的作用整理
2022-09-01 17:08:55
fso对象CreateTextFile方法调用时“无效的过程调用或参数”错误
2009-05-26 15:39:00