[Python] Invalid mode (' W ') or filename: ' D:\x0bideo '

Source: Internet
Author: User

#-*-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 '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.