python文件读写
python 进行文件读写的内建函数是open或file
file_hander(文件句柄或者叫做对象)= open(filename,mode)
mode:
模式 说明
r 只读
r+ 读写
w 写入,先删除源文件,在重新写入,如果文件没有则创建
w+ 读写,先删除源文件,在重新写入,如果文件没有则创建(可以写入写出)
读文件:
>>> fo = open("/root/a.txt") >>> fo
<open file '/root/a.txt', mode 'r' at 0x7f5095dec4e0>
>>> fo.read()
'hello davehe\ni am emily\nemily emily\n'
>>> fo.close() >>> fo.read() #对象已关闭,在读取就读不到
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: I/O operation on closed file
>>> f1 = file("/root/a.txt") >>> f1.read()
'hello davehe\ni am emily\nemily emily\n'
>>> f1.close()
写文件:
root@10.1.6.200:~# ls -l new.txt
ls: cannot access new.txt: No such file or directory
>>> fnew = open("/root/new.txt",'w') w参数文件没有则创建 >>> fnew.write('hello \n i am dave')
这时查看文件数据其实还只是在缓存区中,没有真正落到文件上.
root@10.1.6.200:~# cat new.txt root@10.1.6.200:~#
只要我把文件关闭,数据会从缓存区写到文件里
>>> fnew.close() root@10.1.6.200:~# cat new.txt
hello i am dave
再次使用w参数,文件会被清空,所以用该参数需要谨慎.
>>> fnew = open("/root/new.txt","w")
root@10.1.6.200:~# cat new.txt root@10.1.6.200:~#
mode使用r+参数:
>>> fnew = open("/root/new.txt",'r+') >>> fnew.read()
'hello dave'
>>> fnew.write('i am dave') >>> fnew.close()
root@10.1.6.200:~# cat new.txt
hello davei am dave
这次打开文件,直接写入,会发现ooo替换开头字母,因为上面读取操作使用了指针在写就写在后面.而这次是直接从头写入.
>>> fnew = open("/root/new.txt",'r+') >>> fnew.write('ooo') >>> fnew.close()
root@10.1.6.200:~# cat new.txt
ooolo davei am dave
文件对象方法
下面文件对象方法
- FileObject.close()
- String=FileObject.readline([size])
- List = FileObject.readlines([size])
- String = FileObject.read([size]) read:读取所有数据
- FileObject.next()
- FileObject.write(string)
- FileObject.writelines(List)
- FlieObject.seek(偏移量,选项)
- FlieObject.flush() 提交更新
>>> for i in open("/root/a.txt"): 用open可以返回迭代类型的变量,可以逐行读取数据 ... print i ...
hello davehe i am emily emily emily
FileObject.readline: 每次读取文件的一行,size是指每行每次读取size个字节,直到行的末尾,超出范围会读取空字符串
>>> f1 = open("/root/a.txt") >>> f1.readline()
'hello davehe\n'
>>> f1.readline()
'i am emily\n'
>>> f1.readline()
'emily emily\n'
>>> f1.readline() '' >>> f1.readline() '' >>>f1.close()
FileObject.readlines:返回一个列表
>>> f1 = open("/root/a.txt") >>> f1.readlines()
['hello davehe\n', 'i am emily\n', 'emily emily\n']''
FileObject.next:返回当前行,并将文件指针到下一行,超出范围会给予警示,停止迭代.
>>> f1 = open("/root/a.txt") >>> f1.next()
'hello davehe\n'
>>> f1.next()
'i am emily\n'
>>> f1.next()
'emily emily\n'
>>> f1.next()
Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
FileObject.write:write和后面writelines在写入前会是否清除文件中原来所有的数据,在重新写入新的内容,取决于打开文件的模式.
FileObject.writelines(List):多行写,效率比write高,速度更快,少量写入可以使用write
>>> l = ["python\n","python\n","python\n"] >>> f1 = open('/root/a.txt','a') >>> f1.writelines(l) >>> f1.close()
root@10.1.6.200:~# cat a.txt
hello davehe i am emily emily emily python python python
FlieObject.seek(偏移量,选项):可以在文件中移动文件指针到不同的位置.
位置的默认值为0,代表从文件开头算起(即绝对偏移量),1代表从当前位置算起,2代表从文件末尾算起.
>>> f1 = open('/root/a.txt','r+') >>> f1.read()
'hello davehe\ni am emily\nemily emily\npython\npython\npython\n'
>>> f1.seek(0,0) 指针指到开头,在读 >>> f1.read()
'hello davehe\ni am emily\nemily emily\npython\npython\npython\n'
>>> f1.read() '' >>> f1.seek(0,0) >>> f1.seek(0,2) 指针指到末尾,在读 >>> f1.read() ''
下面看个小实例,查找a.txt中emily出现几次
root@10.1.6.200:~# vim file.py
#!/usr/bin/env python import re f1 = open('/root/a.txt') count = 0 for s in f1.readlines(): li = re.findall("emily",s) if len(li) > 0: count = count + len(li) print "this is have %d emily" % count f1.close()
root@10.1.6.200:~# cat a.txt
hello davehe i am emily emily emily
root@10.1.6.200:~# python file.py
this is have 3 emily

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...

如何解决jieba分词在景区评论分析中的问题?当我们在进行景区评论分析时,往往会使用jieba分词工具来处理文�...

如何使用正则表达式匹配到第一个闭合标签就停止?在处理HTML或其他标记语言时,常常需要使用正则表达式来�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版
中文版,非常好用