as f报错 open with_Python札记2_IO文件操作

本文详细介绍了Python中的文件操作,包括使用open()函数以不同模式(如r、w、a等)打开文件,以及read、readline、readlines等读取文件的方法。还展示了如何处理文件异常,以及使用with语句自动关闭文件。同时,文章讨论了文件编码和不同的写入操作,如write和writelines。最后,提供了一个示例,演示了如何找出两个文件中相同的IP地址。
摘要由CSDN通过智能技术生成

在编程工作中,时常需要对各种文件进行操作。读写文件是最常见的IO编程,Python中内置了读写文件的函数。读写文件是请求系统打开一个文件对象,通常称为文件描述符;然后通过操作系统提供的接口从这个文件对象中读取数据,或者将数据写入文件对象。

廖雪峰官方课程—IO编程

打开文件

打开文件使用open()函数,用读的模式打开返回的是文件对象,它是可迭代的;如果不存在就会报错IOError,标准的语法为:

open(name[,mode[,buffering]])

open函数的文件名是必须的,模式和缓冲参数是可选:f = open('c:\text\a.txt','r') # 用读的模式打开

关闭文件

文件使用完毕必须关闭,因为文件对象会占用操作系统的资源,调用close()函数关闭文件f.close()

如果读写文件出错,close()函数就不会被调用执行。为了保证文件能够正确关闭文件,使用try...finally实现:try:

f = open('/path/to/file', 'r')

print(f.read())

finally:

if f:

f.close()

不同模式

文件打开模式r、w、a对应基本的只读、只写、追加模式;b、t、+、U对应二进制模式、文本模式、读写模式、通用换行符。

英文中文文件是否存在是否清空打开位置

r只读报错不文件开头

r+读+写报错不文件开头

w只写创建清空文件开头

w+写+读创建清空文件开头

a追加写创建不文件结尾

a+追加写+读创建不文件结尾

读取文件

读取文件对象有3种方式:read、 readline、readlines,每种方法接受一个变量以限制每次读取的数据量。3种方法都会把每行末尾的\n读进来,通过strip()进行去除空格。file = open('/home/ubuntu/PycharmProjects/test.txt', 'w')

print(file)

file.close()

# 结果显示为文件对象,用于读操作

<_io.textiowrapper name="/home/ubuntu/PycharmProjects/test.txt" mode="w" encoding="UTF-8">Hello python

this is a test file

count = 0

sum = 0

while count < 3:

sum += 1

print("hello linux)"

count += 1

print("hello python")

1、read

每次读取整个文件,将文件内容放入一个字符串中。如果文件较大,用read(size),指定每次最多读取size个字节。file = open('/home/ubuntu/PycharmProjects/test.txt', 'r')

res1 = file.read()

file.close()

结果:Hello python

this is a test file

count = 0

sum = 0

while count < 3:

sum += 1

print("hello linux)"

count += 1

print("hello python") # str形式

2、readline

每次只读取一行数据,相比较于readlines慢,读取时候占用内存小,适合于大文件,返回的是字符串对象。如果对同一个文件进行多次读取,将会在上次的基础上再进行读取下一行。file = open('/home/ubuntu/PycharmProjects/test.txt', 'r')

res2 = file.readline()

res2 = file.readline()

res2 = file.readline() # 空行也算

res2 = file.readline()

print(res2)

file.close()

结果count = 0 # 结果为第四行的数据,具体看test文件 str形式

3、readlines

读取全部文件内容,自动将文件分成一个行的列表,可用于for...in...迭代获取里面的每个数据。适合读取配置文件file = open('/home/ubuntu/PycharmProjects/test.txt', 'r')

res3 = file.readlines()

print(res3)

file.close()

结果:['Hello python\n', 'this is a test file\n', '\n', 'count = 0 \n', 'sum = 0\n', 'while count < 3:\n', ' sum += 1\n', ' print("hello linux)"\n', ' count += 1\n', 'print("hello python")\n'] # list形式

去掉空行,并用for...in...进行遍历file = open('/home/ubuntu/PycharmProjects/test.txt', 'r')

res3 = file.readlines()

for i in res3:

print(i.strip()) # 去掉空格

file.close()

结果:Hello python

this is a test file

count = 0

sum = 0

while count < 3:

sum += 1

print("hello linux")

count += 1

print("hello python")

with open(...) as f

Python中引入了with语句来自动调用close()方法;传入encoding和errors参数处理编码问题with open('/path/to/file', 'r', encoding='gbk', errors='ignore') as f: # 写入特定编码的文件,传入encoding和error参数

print(f.read()) # 不必再调用close方法

题目:

两个文件中,每个有多行的IP地址,找出两个文件中相同的IP地址import bisect

with open('test1.txt', 'r') as f1:

list1 = f1.readlines()

for i in range(0, len(list1)):

list1[i] = list1[i].strip('\n')

with open('test2.txt', 'r') as f2:

list2 = f2.readlines()

for i in range(0, len(list2)):

list2[i] = list2[i].strip('\n')

list2.sort()

length_2 = len(list2)

same_data = []

for i in list1:

pos = bisect.bisect_left(list2, i)

if pos < len(list2) and list2[pos] == i:

same_data.append(i)

same_data = list(set(same_data))

print(same_data)

写文件

写文件用write()和writelines()来进行操作,用法同read和readlines对应。

write()方法是将字符串写入到文件中。w模式从头开始写入文件

a模式从结尾追加文件

writelines()方法是针对列表的操作。它接收一个字符串列表作为参数,将他们写入到文件中,换行符不会自动的加入,因此,需要显式的加入换行符。f1 = open('test1.txt', 'w')

f1.writelines(["1", "2", "3"]) # 内容为:123

f1 = open('test1.txt', 'w')

f1.writelines(["1\n", "2\n", "3\n"])

# 1

# 2

# 3

编码ASCII:- 最早密码本,只包含字母、数字、特殊字符

- 一个字符一个字节

Unicode- 万国码

- 一个字符需要4个字节

- 浪费资源

utf-8- 最小用8位也就是一个字节表示一个字符

- 字母:一个字节,映射ASCII码

- 欧洲的文字:两个字节

- 中文:三个字节

gbk- 国标:每个国家都有自己的国标

- 字母:一个字节,映射ASCII码

- 中文:两个字节

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值