【发布时间】:2019-10-30 19:43:13
【问题描述】:
我正在尝试编写一些行来将文件从网站下载到计算机上的本地路径,用户通过 ds_dir 提供该路径。如果路径已经存在,我想返回一条错误消息。我当前的代码如下所示,并且是函数的一部分:
#respond to users choice
if choice == "y" or "Y":
if not os.Path.exists(ds_dir):
try:
urllib.request.urlretrieve(url, ds_dir)
print("Downloading...")
return ("you just downloaded the file to: {ds_dir}".format(ds_dir))
except Exception:
log.exception(f"ERROR DURING DOWNLOAD: {ds_dir} FROM {url}.")
现在看来它根本不起作用。 ds_dir 是用户要将其保存到的目录。有人知道如何解决这个问题吗?也可能是一种完全不同的方法。
【问题讨论】:
标签: python user-interface if-statement download urllib