這是我的函數:函數打開.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