python中文件操作的其他方法

前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭。本文中介绍下python中关于文件操作的其他比较常用的一些方法。

首先创建一个文件poems:

p=open(‘poems‘,‘r‘,encoding=‘utf-8‘)for i in p:print(i)结果如下:
hello,everyone白日依山尽,黄河入海流。欲穷千里目,更上一层楼。

1.readline   #读取一行内容

p=open(‘poems‘,‘r‘,encoding=‘utf-8‘)
print(p.readline())

print(p.readline())

结果如下:hello,everyone

白日依山尽,

#这里的两个换行符,一个是everyone后边的\n,一个是print自带的换行

2.readlines   #读取多行内容p=open(‘poems‘,‘r‘,encoding=‘utf-8‘)
print(p.readlines())  #打印全部内容结果如下:[‘hello,everyone\n‘, ‘白日依山尽,\n‘, ‘黄河入海流。\n‘, ‘欲穷千里目,\n‘, ‘更上一层楼。‘]
p=open(‘poems‘,‘r‘,encoding=‘utf-8‘)for i in p.readlines()[0:3]: print(i.strip()) #循环打印前三行内容,去除换行和空格结果如下:hello,world白日依山尽,黄河入海流。

3.tell  #显示当前光标位置
p=open(‘poems‘,‘r‘,encoding=‘utf-8‘)print(p.tell())print(p.read(6))print(p.tell())

结果如下:0hello,64.seek  #可以自定义光标位置
p=open(‘poems‘,‘r‘,encoding=‘utf-8‘)print(p.tell())print(p.read(6))print(p.tell())print(p.read(6))p.seek(0)print(p.read(6))

结果如下:0hello,6everyohello,5.flush  #提前把文件从内存缓冲区强制刷新到硬盘中,同时清空缓冲区。
p=open(‘poems1‘,‘w‘,encoding=‘utf-8‘)p.write(‘hello.world‘)p.flush()p.close()#在close之前提前把文件写入硬盘,一般情况下,文件关闭后会自动刷新到硬盘中,但有时你需要在关闭前刷新到硬盘中,这时就可以使用 flush() 方法。6.truncate  #保留p=open(‘poems‘,‘a‘,encoding=‘utf-8‘)p.truncate(5)p.write(‘tom‘)结果如下:hellotom#保留文件poems的前五个字符,后边内容清空,再加上tom





 
 
 

原文地址:https://www.cnblogs.com/bianhao89757/p/10192453.html

时间: 2024-08-06 20:00:45

python中文件操作的其他方法的相关文章

Python中文件操作

一.文件打开操作 1.文件操作步骤: (1)打开文件模式: f =open("db",'a')    #文件追加 f = open("db",'r')    #只读操作(默认模式) f = open("db",'w')    #只写操作,会先清空原文件 f = open("db",'x')    #文件存在,会报错,不存在创建并只写 f = open("db",'rx|a|w')  #以二进制的方式只读或只

重学Python - Day 05 - python基础 -> python的文件操作:r、w、a、r+、a+ 、readline、readlines 、flush等常用的文件方法

文件的读操作 示例: 1 print("->文件句柄的获取,读操作:") 2 3 f = open('无题','r',encoding='utf8') 4 d = f.read() 5 f.close() 6 print(d) 7 8 print('->例二:') 9 f = open('无题','r',encoding='utf8') 10 e = f.read(9) 11 f.close() 12 print(e) 13 #python3中,文件中一个中英文都占位1 运

python 中文件输入输出及os模块对文件系统的操作

整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作. 文件输入输出 1.内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文件对象. 2.对打开文件进行读取时,readline()与readlines()的区别在于是否一次性的读取所有的内容,并将每行的信息作为列表中的一个子项. 例如:文件test.txt中 1,3,4 2,35,6 分别用readline与readlines对其进行读取 r=file_object.readline(

python基本文件操作(文件输入和输出)

文件输入输出中常用的文件对象方法: open : 返回一个新的文件对象,调用该对象的上的方法可对文件进行任何操作 readline: 读取一行你给哦如果您包括结尾的换行符在内 write: 将数据写入文件中 close: 关闭文件对象:   举例:读取E盘中的work.txt文件 f = open('work.txt') line  =f.readline() print(line) f.close() 如果给readline设置一个参数 n 则读取n个字符 不设置 则只读取一行包括结尾的换行符

python中字符串操作大全

前段时间,闲的蛋疼,重新整理了一下python的学习资料,现在把整理的python中字符串操作分享出来,方便自己和大家今后查询 a = 'SUNW ukong 123456' #print(a.capitalize())      #字符串首字母变成大写 #print(a.casefold())        #将字符串中的大写字母全部变成小写字母 #print(a.center(50,'-'))    #将字符串居中显示,总共50个字符,如果字符串的长度不够50个字符,就在字符串两边补齐‘-’

python之文件操作-复制、剪切、删除等

下面是把sourceDir文件夹下的以.JPG结尾的文件全部复制到targetDir文件夹下: <span style="font-size:18px;">>>>import os >>> import os.path >>> import shutil >>> def copyFiles(sourceDir,targetDir): for files in os.listdir(sourceDir):

Python开发【第三章】:Python的文件操作

Python的文件操作 一.读取操作,3种读取方式的区别 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian info_file = open("here_we_are",encoding="utf-8") #默认读取模式 print(info_file) #不加参数,直接打印 #<_io.TextIOWrapper name='here_we_are' mode='r' encoding='u

python获取文件扩展名的方法(转)

主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧.具体实现方法如下: 1 2 3 4 import os.path def file_extension(path):   return os.path.splitext(path)[1] print file_extension('C:\py\wxPython.gif') 输出结果为:.gif 原文地址:https://www.cnblogs.com/hixiaowei/p/8438930.html

python中的字典内置方法小结

#!/usr/local/bin/python3 # -*- coding:utf-8 -*- #key-value #dict 无序,无下标,不需要下标,因为有key stu={ 'stu001':"zhang yu", 'stu002':"ma hong yan", 'stu003':"zhang guo bin", 'stu004':"sha chun hua" } ''' -----------------------