我需要使用python代码在各种系统上下载并安装Java JDK。我使用了Wget模块,但是我不知道下载文件的目录,因此没有结果
import platform
import requests
import wget
url_windows='https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_windows-x64_bin.exe'
url_mac='https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_osx-x64_bin.dmg'
url_linux='https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_linux-x64_bin.deb'
if platform.system == 'Windows':
wget.download(url_windows)
if platform.system == 'Linux':
wget.download(url_linux)
if platform.system == 'Mac':
wget.download(url_mac)
我该怎么办?
答案 0 :(得分:2)
platform.system是一个函数,应调用它以将其返回值与字符串进行比较,因此请改用platform.system()。到目前为止,实际上因为所有比较都失败,所以您并未在代码中的任何地方使用wget。
答案 1 :(得分:2)
默认情况下,它被下载到运行它的当前目录中。
请参见help(wget.download)
:
Help on function download in module wget:
download(url, out=None, bar=<function bar_adaptive at 0x100ddcdd0>)
High level function, which downloads URL into tmp file in current
directory and then renames it to filename autodetected from either URL
or HTTP headers.
:param bar: function to track download progress (visualize etc.)
:param out: output filename or directory
:return: filename where URL is downloaded to
它返回下载文件的文件名,因此您可以使用os.path.abspath
来获取完整路径:
>>> import os
>>> import wget
>>> f = wget.download("https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_osx-x64_bin.dmg")
100% [................................................................................] 5307 / 5307>>>
>>> f
'jdk-13.0.1_osx-x64_bin.dmg'
>>> os.path.abspath(f)
'/Users/gino/jdk-13.0.1_osx-x64_bin.dmg'
>>>
该函数还接受一个out
参数,以便您可以显式设置下载路径。
作为旁注,如@CrazyElf's answer所述,您应该使用platform.system()
来获取操作系统(“ Windows”,“ Darwin”等)的字符串名称,而不是{{ 1}}是一个功能:
platform.system
(请注意,它在Mac上返回“ Darwin”,而不是“ Mac”。)