使用 file.info()
file.info("data/ullyses.txt")
size isdir mode mtime ctime atime uid gid
data/ullyses.txt 1573151 FALSE 664 2015-06-01 15:25:55 2015-06-01 15:25:55 2015-06-01 15:25:55 1008 1008
然后提取名为size
的列:
file.info("data/ullyses.txt")$size
[1] 1573151
download.file()
函数下载文件,然后在本地检查文件大小。 - Andriefile.size()
包装器函数。 - Scarabee也许在这次讨论之后做了更新,但至少在 R3.4+ 中,答案是 file.size
。
library(RCurl)
url = "http://math.ucdenver.edu/RTutorial/titanic.txt"
xx = getURL(url, nobody=1L, header=1L)
strsplit(xx, "\r\n")
file_url = 'http://math.ucdenver.edu/RTutorial/titanic.txt'
curl_cmd = paste('curl -X HEAD -i', file_url)
system_cmd = paste(curl_cmd, '|grep Content-Length |cut -d : -f 2')
system()
执行。 curl_cmd
字符串告诉curl只获取文件的标题。
system_cmd
字符串添加了一些额外的命令来解析头并提取文件大小。system()
并使用intern = TRUE
参数告诉R保留输出。b <- system(system_cmd, intern = TRUE)
## % Total % Received % Xferd Average Speed Time Time Time Current
## Dload Upload Total Spent Left Speed
## 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
## curl: (18) transfer closed
b
将是以字节为单位的文件大小。
print(paste("There are", as.numeric(b)/1e6, "mb in the file:", file_url))
## [1] "There are 0.055692 mb in the file: http://math.ucdenver.edu/RTutorial/titanic.txt"
RCurl
中摆弄了大约五分钟,但没什么进展。 - neerajtfile.size
之外,您还可以使用fs
包中的file_size
,它将以更易读的方式显示文件大小,显示MB或GB而不是字节。library(fs)
file.size(system.file("data/Rdata.rdb", package = "datasets"))
#> [1] 114974
fs::file_size(system.file("data/Rdata.rdb", package = "datasets"))
#> 112K
file.size(system.file("data/Rdata.rdb", package = "spData"))
#> [1] 2676333
fs::file_size(system.file("data/Rdata.rdb", package = "spData"))
#> 2.55M
?file.info
可能是你想要的。 - cory