#-*-coding:utf-8-*-import stringimport jsonimport urllib2import urllibimport base64import sysreload (SYS) SYS.SETDEFAU Ltencoding ("Utf-8") #调用优酷网API获取json数据def Getyouku (): req=urllib2. Request (Url,code) Data=urllib2.urlopen (req). Read () obj=json.loads (data) #把json格式字符串解码转换成Python对象 newobj=json.dumps (obj,indent=4,ensure_ascii=false) #把Python对象编码转换成json字符串, indent, show Chinese return newobj# Write JSON data to file Def jsonfile (newobj): File=open ("D:\video.json", "W") File.write (newobj) file.close () #开始if __name__ = = ' __main__ ': url= "Https://openapi.youku.com/v2/searches/video/by_keyword.json" params={' client_id ': ' Xxxxxxxxx ', ' keyword ': ' Ice bucket Challenge ', ' category ': ' Entertainment '} Code=urllib.urlencode (params) newobj= Getyouku () jsonfile (newobj)
"Problem"
"Resolution"
This error occurs when you use the built-in function file () or open (). Either because the file's open mode is incorrect, or if there is a problem with the filename.
The former only needs to pay attention to whether the file can be read or writable. The latter is a file-path-related issue that needs to be escaped with R or R before the file name,
such as: File (r "D:\video.json", ' W '). or turn the backslash \ into two, such as file ("D:\\video.json", ' W ').
[Python] Invalid mode (' W ') or filename: ' D:\x0bideo '