python open 函数的一些坑

(1)路径问题

open一个同py文件同一个目录的文件的时候,用以下:

txt = open('/filtered_words.txt','rb')
words = txt.readline()
filtered = []
for word in words:
filtered.append(word)
txt.close()
print(filtered)

会报错
FileNotFoundError: [Errno 2] No such file or directory: '/filtered_words.txt'

这里写错了一个地方,应该写成
txt = open('filtered_words.txt','rb')
也可利用绝对路径

txt = open('E:\python_project\testgithub\filtered_words.txt','rb')
这样也不会报错
用反斜杠,这样也不会报错

txt = open('E:/python_project/test/github/filtered_words.txt','rb')

(3)打开文件格式

文档的内容是汉字,用utf-8格式,如果用rb权限open文件,汉字打印出来会变成数字

txt = open('filtered_words.txt','rb')
words = txt.readline()
打印结果是:
[229, 140, 151, 228, 186, 172, 13, 10]

 

用r权限open会报错

UnicodeDecodeError: 'gbk' codec can't decode byte 0x98 in position 16: illegal multibyte sequence
在open的时候,加一个编码

txt = open('filtered_words.txt','r',encoding='UTF-8')
words = txt.readline()
打印出来的结果是
['', '', '
']

把readline换成read函数,打印结果是

['', '', '
', '', '', '', '
', '', '', '']

自行百度了read()、readline()、readlines()的区别,最后代码整理

txt = open('filtered_words.txt','rb',encoding='UTF-8')
wor = txt.readlines()
filtered = []
for word in wor:
word = word.strip('
')
filtered.append(word)
print(word)
txt.close()
print(filtered)
打印结果是['北京', '程序员', '公务员']


最终代码如下:
class senseWord():
def __init__(self):
self.list = []
file = open('filtered_words.txt','r',encoding='UTF-8')
words = file.readlines()
for word in words:
word = word.strip('
')
self.list.append(word)

def checkwords(self,str):
if str in self.list:
return True
else:
return False


if __name__ == '__main__':
sense = senseWord()
str = input('input a string')
if sense.checkwords(str) == True:
print('freedom')
else:

————————————————

原文地址:https://www.cnblogs.com/Rivend/p/11785078.html