朱莉娅:http.download函数可以创建一个目录,而不是存在吗?

发布于 2025-01-29 07:11:30 字数 577 浏览 2 评论 0原文

此代码失败是因为../ data/housing/目录不存在:

function FetchHousingData(url::String, path::String)
    println("FetchHousingData " * Dates.format(now(), "HH:MM:SS") )
    HTTP.download(url, path)
    println("done "  * Dates.format(now(), "HH:MM:SS") )
end

housingUrl = "https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.tgz"
housingPathFile = "../data/housing/housing.tgz"

FetchHousingData(housingUrl, housingPathFile)

有没有办法让http.download()创建一个缺少的目录?我找不到http.download的文档。

This code failed because the ../data/housing/ directory didn't exist:

function FetchHousingData(url::String, path::String)
    println("FetchHousingData " * Dates.format(now(), "HH:MM:SS") )
    HTTP.download(url, path)
    println("done "  * Dates.format(now(), "HH:MM:SS") )
end

housingUrl = "https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.tgz"
housingPathFile = "../data/housing/housing.tgz"

FetchHousingData(housingUrl, housingPathFile)

Is there a way to have HTTP.download() create a missing directory? I haven't been able to find the docs for HTTP.download.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2025-02-05 07:11:30

您可以通过输入帮助模式从repper中获取DOCS(在REPP提示下按):

help?> HTTP.download
  download(url, [local_path], [headers]; update_period=1, kw...)

  Similar to Base.download this downloads a file, returning the filename. If the local_path:

    •  is not provided, then it is saved in a temporary directory

    •  if part to a directory is provided then it is saved into that directory

    •  otherwise the local path is uses as the filename to save to.

  When saving into a directory, the filename is determined (where possible), from the rules of the HTTP.

    •  update_period controls how often (in seconds) to report the progress.
       • set to Inf to disable reporting

    •  headers specifies headers to be used for the HTTP GET request

    •  any additional keyword args (kw...) are passed on to the HTTP request.

http.http.http.download似乎没有Kwarg要创建目录,但您可能对isdir

help?> isdir

  isdir(path) -> Bool

  Return true if path is a directory, false otherwise.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> isdir(homedir())
  true
  
  julia> isdir("not/a/directory")
  false

  See also isfile and ispath.

mkdir

help?> mkdir

  mkdir(path::AbstractString; mode::Unsigned = 0o777)

  Make a new directory with name path and permissions mode. mode defaults to 0o777, modified by the current file creation mask. This function never creates more than one directory. If the directory already exists, or some intermediate
  directories do not exist, this function throws an error. See mkpath for a function which creates all required intermediate directories. Return path.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> mkdir("testingdir")
  "testingdir"
  
  julia> cd("testingdir")
  
  julia> pwd()
  "/home/JuliaUser/testingdir"

You can get the docs from the REPL as usual by entering help mode (press ? at the REPL prompt):

help?> HTTP.download
  download(url, [local_path], [headers]; update_period=1, kw...)

  Similar to Base.download this downloads a file, returning the filename. If the local_path:

    •  is not provided, then it is saved in a temporary directory

    •  if part to a directory is provided then it is saved into that directory

    •  otherwise the local path is uses as the filename to save to.

  When saving into a directory, the filename is determined (where possible), from the rules of the HTTP.

    •  update_period controls how often (in seconds) to report the progress.
       • set to Inf to disable reporting

    •  headers specifies headers to be used for the HTTP GET request

    •  any additional keyword args (kw...) are passed on to the HTTP request.

There doesn't seem to be a kwarg in HTTP.download to create directories, but you might be interested in isdir:

help?> isdir

  isdir(path) -> Bool

  Return true if path is a directory, false otherwise.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> isdir(homedir())
  true
  
  julia> isdir("not/a/directory")
  false

  See also isfile and ispath.

and mkdir:

help?> mkdir

  mkdir(path::AbstractString; mode::Unsigned = 0o777)

  Make a new directory with name path and permissions mode. mode defaults to 0o777, modified by the current file creation mask. This function never creates more than one directory. If the directory already exists, or some intermediate
  directories do not exist, this function throws an error. See mkpath for a function which creates all required intermediate directories. Return path.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> mkdir("testingdir")
  "testingdir"
  
  julia> cd("testingdir")
  
  julia> pwd()
  "/home/JuliaUser/testingdir"
身边 2025-02-05 07:11:30

我更喜欢使用mkPath(在NILS显示的文档中也提到),因为它会创建父目录,并且如果目录已经存在,则不会产生错误:

housingPathFile = "../data/housing/housing.tgz"
mkpath(dirname(housingPathFile))

I prefer to use mkpath (also mentioned in the docs showed by Nils) as it creates parent directories and does not produce an error if the directory already exists:

housingPathFile = "../data/housing/housing.tgz"
mkpath(dirname(housingPathFile))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文