您的位置:首页 > 编程语言 > Python开发

Python文件基本操作

2016-04-21 17:07 525 查看
打开文件:
  file_obj = file("文件路径","模式") #2.0版本的
file_obj = open("文件路径",“模式”) #3.0版本的
打开文件的模式有:

r,以只读方式打开文件

w,打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。

a,打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。

w+,打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。

写入文件内容:

file_obj.write("文件内容")

关闭文件句柄:

file.obj.close() #注意每一次文件操作都要关闭文件句柄,不然报错

#打开文件f.txt,写入内容,关闭文件
f = open("f.txt","w")
f.write("This is the first line\n")
f.write("This is the second line\n")
f.write("This is the third line\n")
f.write("This is the 4 line\n")
f.write("This is the 5 line\n")
f.close()


读取文件内容:

file_obj.read() #一次性加载文件所有内容到内存

#读文件所有内容
f = open("f.txt","r")
aa = f.read()
print(aa)
f.close()

#执行结果:
This is the first line
This is the second line
This is the third line
This is the 4 line
This is the 5 line


file_obj.readlines() #一次性加载文件所有内容到内存,并根据行分割成字符串

#读文件所有内容,结果分割成了列表形式
f = open("f.txt","r")
aa = f.readlines()
print(aa)
f.close()

#执行结果:
['This is the first line\n', 'This is the second line\n', 'This is the third line\n', 'This is the 4 line\n', 'This is the 5 line\n']


#一行行显示输出
f = open("f.txt","r")
for line in f:
print(line)
f.close()

#执行结果:其中有空行是因为写了换行符
This is the first line

This is the second line

This is the third line

This is the 4 line

This is the 5 line


应用:

#判断字符是否存在行
f = open("f.txt","r")
for line in f:
if "5" in line:
print("this is the five line")
else:
print(line)
f.close()

#执行结果:
This is the first line

This is the second line

This is the third line

This is the 4 line

this is the five line


追加文件内容:

file_obj = open("f.txt","a")

#追加
f = open("f.txt","a")
f.write("7")
f.write("8")
f.close()

#文件内容:
This is the first line
This is the second line
This is the third line
This is the 4 line
This is the 5 line
78


文件打开方法二:用with就可以不需要加关闭文件f.close()了

#另外一种文件打开方式
file2 = "f.txt"
with open(file2,"r") as f:
aa = f.read()
print(aa)

执行结果:
This is the first line
This is the second line
This is the third line
This is the 4 line
This is the 5 line
78


"+" 表示可以同时读写某个文件

r+,可读写文件。【可读;可写;可追加】

w+,写读

a+,同a

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

rU

r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

rb

wb

ab

注意:3.0版本的f.read()读的是字符(2.7读的是字节);f.tell()指定的是指针位置(读的是字节),f.seek()按照字节操作

如果结合使用有点坑,因为一个中文代表3个字节。

补充:如果3.0版本打开文件模式是“rb”,则都是按照字节操作。

#
f = open("test.log","w")
f.write("无ddddd")
f.close()

f = open("test.log","r")
ret = f.read(2)      #指定读取2个字符,默认多读 f.close()

#执行结果:
无d


#查看当前指针位置,默认在0位置
f = open("test.log","r",encoding="utf -8")
print(f.tell())     #指针的默认位置在0位置
f.close()

f = open("test.log","r",encoding="utf -8")
f.read(2)         #按照字符读
print(f.tell())     #因为第一个是中文,指针指向字节位置4,按照字节读,查看当前指针位置
f.close()

#执行结果:
#由于第一个是中文3个字节,指针在位置2,f.tell()读出来的字节在中文中,会报错(即也可以说乱码了)


#指定当前指针位置
f = open("test.log","r",encoding="utf -8")
f.seek(1)       #指定当前指针位置
f.read()         #按照字符读
ret = f.read()     #因为第一个中文是3个字节,所以会报错
f.close()
print(ret)

#执行结果:由于指定指针在中文里,所以报错了


#在原文件保留指针前面的数据,后面的删除
f = open("test.log","r",encoding="utf -8")
f.seek(5)
print(f.read())    #读指针后面的,并不改变原文件
f.close()

f = open("test.log","r+",encoding="utf -8")   #需开放可读写权限
f.seek(5)
f.truncate()      #删除了原文件指针5后面的数据,保留了前面的,改变了原文件
f.close()


#补充:

从一个文件一行行读取内容写到新文件中


#从一个文件一行行读取内容写到新文件中
with open("test.log") as read_file,open("new_test.log","w") as write_file:
for line in read_file:
write_file.write(line)
aa = line.startswith("back")    #判断字符串头是否匹配“back”
print(aa)     #返回True或者False


python读文件时按字符还是子节?

#python2.0版本
f = open('ha.log','r')
data = f.read()
f.tell() # 按照字节进行操作
f.close()

#python3.0版本
f = open('ha.log','r')
data = f.read()
f.tell() # 按照字符进行操作
f.seek(5)   #按照字节进行操作
f.close()

f = open('ha.log','rb')
data = f.read()
f.tell() # 按照字节进行操作
f.close()


字节和字符串转换问题:

utf-8格式中,一个中文=3个字节;1个字节=8bit

#3.0版本,字节和字符串可以直接互相转换
s = "吴佩琪"
for item in s:
print(item)    #for循环,字符循环;2.0版本是以字节循环的
s_bytes = bytes(s,"utf8")
print(s_bytes)      #字符串转换为字节;2.0版本str==bytes,形同虚设
new_str = str(s_bytes,"utf8")
print(new_str)      #字节转换为字符串

#输出结果:
吴
佩
琪
b'\xe5\x90\xb4\xe4\xbd\xa9\xe7\x90\xaa'
吴佩琪
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: