Python 为什么开放功能不起作用?
我无法理解代码中发生了什么。它无法打开文件夹Python 为什么开放功能不起作用?,python,Python,我无法理解代码中发生了什么。它无法打开文件夹 import urllib def read_text (): quotes = open ("C:\Users\HP\Downloads\book\just.txt") contents_of_file = quotes.read() print("content_of_file") quotes.close() check_profanity(contents_of_file) def check_p
import urllib
def read_text ():
quotes = open ("C:\Users\HP\Downloads\book\just.txt")
contents_of_file = quotes.read()
print("content_of_file")
quotes.close()
check_profanity(contents_of_file)
def check_profanity(text_to_check):
connection = urllib.urlopen("http://www.wdylike.appspot.com/?q="+text_to_check)
output = connecting.read()
print(output)
connection.close()
read_text()
文件路径包含字符\U,该字符在Python Unicode中具有特殊含义。您可能收到类似以下内容的错误:
File "C:/Python34/Testing.py",
quotes = open ("C:\Users\HP\Downloads\book\just.txt")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
发生这种情况时,需要使用双反斜杠:
def read_text():
quotes = open("C:\\Users\HP\Downloads\book\just.txt")
您收到了什么错误消息?当您运行代码时,是否收到任何错误?请稍候。您确定它应该是output=connecting.read而不是output=connection.read吗?连接不存在,因此我猜您会遇到名称错误,除非您向我们展示的代码中只包含一个打字错误。@Fahimshariar您得到的完整错误回溯是什么?@seanfrancis.Ballais错误消息应以文本形式提供,而不是以屏幕截图的形式提供===========重新启动:C:/Users/HP/Downloads/python/check_-profanity.py========回溯most最近一次调用:文件C:/Users/HP/Downloads/python/check_-profanity.py,第16行,在read_文本文件C:/Users/HP/Downloads/python/check_-profanity.py,第4行,在read_-text-quotes=open C:\\Users\HP\Downloads\book\just.txt OSError:[Errno 22]无效参数:“C:\\Users\\HP\\Downloads\x08ook\\just.txt'@fahimsharir,我想你错过了在“下载”和“书籍”之间添加一个额外的斜杠。它应该是C:\\Users\HP\Downloads\\book\\just.txt.@Sean Francis N.Ballais文件内容回溯最近一次调用:文件C:/Users/HP/Downloads/python/check_-profanity.py,第17行,在读取文本文件C:/Users/HP/Downloads/python/check_-profanity.py,第9行,在文件C:/Users/HP/Downloads/python/check\u profanity.py的第12行,check\u profanity connection=urllib中的read\u text check\u profanity contents\u中。urlopen@FahimShahriar,这是完整的回溯吗?另外,试着用正斜杠替换反斜杠。@seanfrancis.Ballais,我也试过了。但它不起作用