I am trying to make a script that downloads a file, but it keeps getting the error urllib.error.URLError: <urlopen error unknown url type: c>
after I do something like download.py https://i.sstatic.net/tFuiz.png
Here is my script:
import os
import wget
import sys
url = sys.argv[0]
directory = os.path.expanduser('~')
downloadFolderPath = r"%s\Downloads" % directory
os.chdir(downloadFolderPath)
url.replace(":", "%3A")
wget.download(url)
Is there any way I can fix this?
import os
import wget
import sys
url = sys.argv[1]
directory = os.path.expanduser('~')
downloadFolderPath = os.path.join(directory, "Downloads")
os.chdir(downloadFolderPath)
url.replace(":", "%3A")
wget.download(url)
The problem is that you are using the first argument as the url. argv[0]
is the script name, not the url you pass as an argument. See: sys.argv documentation:
The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not).
Most likely you are getting the error because you are using Windows and the first argument is the full pathname, e.g. c:\scripts\download.py
or the like.
If you change it to sys.argv[1]
and call the script with
python download.py https://i.sstatic.net/tFuiz.png
(replace download.py
with the name of your script) then it should work.
Note: I have also changed downloadFolderPath
. By using os.path.join()
the script should work independently of the operating system. On Ubuntu for example your version would not work because of the backslash in the path.