这是我的函数:函数打开.txt文件和打印内容不工作
def read_text():
quotes = open("C:\blop\movie_quotes.txt")
contents_of_file = quotes.read
print(contents_of_file)
quotes.close
read_text()
我只是想读取文件和打印文件中的文本,但我得到这个错误:
Traceback (most recent call last):File "C:/Python27/detect_profanity.py", line 6, in <module>
read_text()File "C:/Python27/detect_profanity.py", line 2, in read_text
quotes = open("C:\blop\movie_quotes.txt")
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\x08lop\\movie_quotes.txt'
我该如何解决?
除了使用'原始字符串' - 'r“C:\ blop \ movie_quotes.txt”',你应该使用[with open](http://stackoverflow.com/questions/9282967/how-to-open- a-file-using-the-open-with-statement)而不是仅仅“打开” – Vaulstein