Python文件操作类操作实例详解

yipeiwu_com5年前Python基础

本文讲述了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中让MySQL查询结果返回字典类型的方法

Python的MySQLdb模块是Python连接MySQL的一个模块,默认查询结果返回是tuple类型,只能通过0,1..等索引下标访问数据 默认连接数据库: 复制代码 代码如下: M...

Windows系统Python直接调用C++ DLL的方法

环境:Window 10,VS 2019, Python 2.7.12, 64bit 1,打开 VS 2019,新建C++ Windows 动态链接库工程 Example,加入下列文件,...

Python ldap实现登录实例代码

下面一段代码是小编给大家介绍的Python ldap实现登录实例代码,一起看看吧 ldap_config = { 'ldap_path': 'ldap://xx.xx.xx.xx...

python中的句柄操作的方法示例

python中的句柄操作的方法示例

通过窗口标题获取句柄 import win32gui hld = win32gui.FindWindow(None,u"Adobe Acrobat") #返回窗口标题为Adobe...

Python基础之函数原理与应用实例详解

本文实例讲述了Python基础之函数原理与应用。分享给大家供大家参考,具体如下: 目标 函数的快速体验 函数的基本使用 函数的参数 函数的返回值 函数的嵌套调用...