初学python,使用open函数的路径一些坑

做一个小练习:敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。

北京
程序员
公务员

(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\\test\github\\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()

打印出来的结果是

['北', '京', '\n']

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

['北', '京', '\n', '程', '序', '员', '\n', '公', '务', '员']

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

txt = open('filtered_words.txt','rb',encoding='UTF-8')
wor = txt.readlines()
filtered = []
for word in wor:
    word = word.strip('\n')
    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('\n')
            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:
        print('human rights')




你可能感兴趣的:(python,python,open,文件操作)