gpt4 book ai didi

r - 下载后在R中解压文件

转载 作者:行者123 更新时间:2023-12-05 04:31:36 24 4
gpt4 key购买 nike

我正在尝试使用 R 在下载后解压缩文件。它在 Windows 10 上可以很好地解压缩。

verbose <- T
zipdir <- file.path("downloads","zip")
datadir <- file.path("downloads","data")


if (!file.exists("downloads")) dir.create("downloads")
if (!file.exists(zipdir)) dir.create(zipdir)
if (!file.exists(datadir)) dir.create(datadir)


filename <- "On_Time_Reporting_Carrier_On_Time_Performance_1987_present_2019_2.zip"

fileurl <- str_c("https://transtats.bts.gov/PREZIP/",filename)

if (verbose == TRUE) print(str_c("File url: ",fileurl))

zipfile <- file.path(zipdir, filename)
if (verbose == TRUE) print(str_c("File: ",zipfile))

download.file(fileurl, zipfile)
unzip(zipfile)

zip 文件的错误 1 ​​表示“不允许操作”

Warning message:
In unzip(zipfile) : error 1 in extracting from zip file

最佳答案

这是在 r2evans 的帮助下的解决方案:

download.file(fileurl, zipfile, mode = wb)

unzip(zipfile, exdir=datadir, overwrite=TRUE)

完整代码来了,复制试试

verbose <- T
zipdir <- file.path("downloads","zip")
datadir <- file.path("downloads","data")


if (!file.exists("downloads")) dir.create("downloads")
if (!file.exists(zipdir)) dir.create(zipdir)
if (!file.exists(datadir)) dir.create(datadir)


filename <- "On_Time_Reporting_Carrier_On_Time_Performance_1987_present_2019_2.zip"

fileurl <- str_c("https://transtats.bts.gov/PREZIP/",filename)

if (verbose == TRUE) print(str_c("File url: ",fileurl))

zipfile <- file.path(zipdir, filename)
if (verbose == TRUE) print(str_c("File: ",zipfile))

#These are the modified lines in the code

#Mode = wb is required to download binary files

download.file(fileurl, zipfile, mode = wb)

#Changed the function so that it specifies the target directory
#I recommend overwrite=TRUE otherwise it might crash. Alternative would be to check with file.exists

unzip(zipfile, exdir=datadir, overwrite=TRUE)

关于r - 下载后在R中解压文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71810738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com