尝试创建文件时出现ValueError未知的URL类型

时间:2019-05-06 17:31:14

标签: python python-3.x

我使用open(os.path.abspath(os.curdir) + "/children.csv", "w")创建数据并将其写入当前目录中的文件中。当我运行此命令时,出现以下错误:

ValueError: unknown url type: '/Users/EL-CAPITAN-2016/Downloads/children.csv'

我尝试仅使用open("children.csv","w"),但它给了我同样的错误。 我尝试手动创建文件,并使用其URL来使用open("/Users/EL-CAPITAN-2016/Downloads/children.csv","r")读取文件,但还是出现相同的错误

用于创建并写入文件的代码

from urllib.request import urlopen as open
import os
print(os.path.abspath(os.curdir))
f = open(os.path.abspath(os.curdir) + '/children.csv','w')

我用来读取我手动创建的文件的代码

from urllib.request import urlopen as open
import os
print(os.path.abspath(os.curdir))
f = open('/Users/EL-CAPITAN-2016/Downloads/children.csv','r')

我收到的错误日志如下

Traceback (most recent call last):
  File "children.py", line 7, in <module>
    f = open('children.csv','r')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 510, in open
    req = Request(fullurl, data)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 328, in __init__
    self.full_url = url
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 354, in full_url
    self._parse()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '/Users/EL-CAPITAN-2016/Downloads/children.csv''

1 个答案:

答案 0 :(得分:0)

Shuvojit&OP指出,问题是将urlopen导入为open代替了内置open

from urllib.request import urlopen as open