问题
NOTE: the first part of "code" isn't code, it's error message from terminal, SE made me format it that way.
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'
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?
回答1:
Second parameter of urlretrieve
should be a path to a file not to a directory.
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
Copy a network object denoted by a URL to a local file, if necessary.
You may fix it like:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/download.temp")
回答2:
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:
urllib.urlretrieve(text_in_clipboard, "/home/thomasshera/Pictures/Star Wars/data.txt")
回答3:
First off, the error you see has nothing to do with line 94 of your script, rather it's in urllib.py
module.
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:
IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'
Please, read urllib docs carefully.
来源:https://stackoverflow.com/questions/34601758/python-ioerror-errno-21-is-a-directory-home-thomasshera-pictures-star-war