Python "IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'"
Python "IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'"
注意:"code" 的第一部分不是代码,它是来自终端的错误消息,SE 让我这样格式化。
Linux 机器使用 Python 2.7.6
我有下载自动程序,它应该获取剪贴板内容(有效)并下载它们(无效)。这是错误消息:
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")
使用 Python shell 可以直接解决这个问题,只是我的程序没有第 94 行 - shell 有什么问题?
错误不是发生在您的代码中,而是发生在您使用的库中 - 所以为什么您会看到文件 /usr/lib/python2.7/urllib.py 的第 94 行。但它的原因在你的代码中。 urllib.urlretrieve
将文件名(不是文件夹名)作为第二个参数。 (在回溯中我们看到它被传递给需要文件名的函数 open
)。这将起作用:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/data.txt")
urlretrieve
的第二个参数应该是文件路径而不是目录。
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
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")
首先,您看到的错误与脚本的第 94 行无关,而是在 urllib.py
模块中。
其次,问题在于您如何使用 urlretrieve
方法。它接受一个 URL 和一个文件名。但是您使用的是目录路径,而不是文件路径。 urllib
准确地告诉你我刚才说的话:
IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'
请仔细阅读urllib docs。
注意:"code" 的第一部分不是代码,它是来自终端的错误消息,SE 让我这样格式化。
Linux 机器使用 Python 2.7.6
我有下载自动程序,它应该获取剪贴板内容(有效)并下载它们(无效)。这是错误消息:
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")
使用 Python shell 可以直接解决这个问题,只是我的程序没有第 94 行 - shell 有什么问题?
错误不是发生在您的代码中,而是发生在您使用的库中 - 所以为什么您会看到文件 /usr/lib/python2.7/urllib.py 的第 94 行。但它的原因在你的代码中。 urllib.urlretrieve
将文件名(不是文件夹名)作为第二个参数。 (在回溯中我们看到它被传递给需要文件名的函数 open
)。这将起作用:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/data.txt")
urlretrieve
的第二个参数应该是文件路径而不是目录。
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
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")
首先,您看到的错误与脚本的第 94 行无关,而是在 urllib.py
模块中。
其次,问题在于您如何使用 urlretrieve
方法。它接受一个 URL 和一个文件名。但是您使用的是目录路径,而不是文件路径。 urllib
准确地告诉你我刚才说的话:
IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'
请仔细阅读urllib docs。