我使用 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''
最佳答案
正如 Shuvojit 和 OP 所发现的那样,问题在于将 urlopen
导入为 open
替换了内置 open
from urllib.request import urlopen as open
关于python - 尝试创建文件时出现 ValueError 未知 url 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56009869/