【问题标题】:Use urlretrieve to download file from url if not yet existing如果不存在,请使用 urlretrieve 从 url 下载文件
【发布时间】: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


    【解决方案1】:

    你的 if 语句是正确的,但你拼错了 'path':

    应该是:

    os.path.exists(ds_dir)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-06
      • 2014-02-25
      • 1970-01-01
      • 2021-02-19
      • 2018-04-16
      • 1970-01-01
      • 2015-09-12
      • 2017-01-31
      相关资源
      最近更新 更多