Python“IOError:[Errno 21]是一个目录:'/home/thomasshera/Pictures/Star Wars'" [英] Python "IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'"
问题描述
注意:代码"的第一部分不是代码,它是来自终端的错误消息,SE 让我这样格式化.
NOTE: the first part of "code" isn't code, it's error message from terminal, SE made me format it that way.
使用 Python 2.7.6 的 Linux 机器
Linux machine using Python 2.7.6
我有下载自动机,它应该获取剪贴板内容(这有效)并下载它们(这不起作用).这是错误信息:
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'
代码如下:
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")
除了我的程序没有第 94 行之外,使用 Python shell 修复这个问题很简单——shell 不知何故出错了?
This would be straightforward to fix using Python shell except that there is no line 94 of my program- the shell is somehow wrong?
推荐答案
urlretrieve
的第二个参数应该是文件的路径,而不是目录.
Second parameter of urlretrieve
should be a path to a file not to a directory.
urllib.urlretrieve(url[,文件名[, reporthook[, data]]])
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
如有必要,将 URL 表示的网络对象复制到本地文件.
Copy a network object denoted by a URL to a local file, if necessary.
您可以像这样修复它:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/download.temp")
这篇关于Python“IOError:[Errno 21]是一个目录:'/home/thomasshera/Pictures/Star Wars'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!