Python“IOError:[Errno 21]是一个目录:'/home/thomasshera/Pictures/Star Wars'”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34601758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python "IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'"
提问by Thomas Shera
NOTE: the first part of "code" isn't code, it's error message from terminal, SE made me format it that way.
注意:“代码”的第一部分不是代码,它是来自终端的错误消息,SE 让我以这种方式对其进行格式化。
Linux machine using Python 2.7.6
使用 Python 2.7.6 的 Linux 机器
I have download automator which is supposed to take clipboard contents (this works) and download them (this doesn't work). Here is error message:
我有下载自动机,它应该获取剪贴板内容(这有效)并下载它们(这不起作用)。这是错误消息:
Traceback (most recent call last):
File "/home/thomasshera/Create polynomial from random sequence.py", line 6, in <module>
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars")
File "/usr/lib/python2.7/urllib.py", line 94, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
File "/usr/lib/python2.7/urllib.py", line 244, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'
Here is code:
这是代码:
import Tkinter
import urllib
root = Tkinter.Tk()
root.withdraw() # Hide the main window (optional)
text_in_clipboard = root.clipboard_get()
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars")
This would be straightforward to fix using Python shell except that there is no line 94 of my program- the shell is somehow wrong?
除了我的程序没有第 94 行之外,使用 Python shell 修复这将很简单——shell 不知何故是错误的?
采纳答案by bigOther
Second parameter of urlretrieve
should be a path to a file not to a directory.
的第二个参数urlretrieve
应该是文件的路径而不是目录。
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
Copy a network object denoted by a URL to a local file, if necessary.
urllib.urlretrieve(url[, 文件名[, 报告钩子[, 数据]]])
如有必要,将由 URL 表示的网络对象复制到本地文件。
You may fix it like:
你可以像这样修复它:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/download.temp")
回答by Eugene Primako
Error occurs not in your code, but in the library you use - so why you see line 94 of file /usr/lib/python2.7/urllib.py. But the reason of it is in yours code. urllib.urlretrieve
takes filename (not folder name) as its second argument. (In traceback we see that it is passed to function open
which expects filename). This will work:
错误不是发生在您的代码中,而是发生在您使用的库中 - 那么为什么您会看到文件 /usr/lib/python2.7/urllib.py 的第 94 行。但它的原因在你的代码中。urllib.urlretrieve
将文件名(不是文件夹名)作为它的第二个参数。(在回溯中,我们看到它被传递给open
需要文件名的函数)。这将起作用:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/data.txt")
回答by vrs
First off, the error you see has nothing to do with line 94 of your script, rather it's in urllib.py
module.
首先,您看到的错误与脚本的第 94 行无关,而是在urllib.py
模块中。
Secondly, the problem is how you are using urlretrieve
method. It takes in an URL and a filename. But you're using path to a directory, rather than a file. And urllib
tells you exactly what I just said:
其次,问题在于你如何使用urlretrieve
方法。它接受一个 URL 和一个文件名。但是您使用的是目录的路径,而不是文件。并urllib
准确地告诉你我刚刚说的话:
IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'
Please, read urllib docscarefully.
请仔细阅读urllib 文档。