【问题标题】:How to check if an argument is a file or a directory in R?如何检查参数是R中的文件还是目录?
【发布时间】:2015-08-07 01:21:42
【问题描述】:

我非常熟悉 python 等价物 'os.path.isfile(path)' 和 'os.path.isdir(path)' 如果 path 是文件或目录。

到目前为止,我还没有在 R 中找到一种简单直接的方法来做到这一点,而且似乎连 Google 也从未听说过这个!

有没有人能打败谷歌并告诉我在 R 中检查给定参数是文件、目录还是它们都不是的最简单方法是什么?

【问题讨论】:

    标签: python r if-statement


    【解决方案1】:

    扩展评论:

    file.info("/etc")
    ##      size isdir mode               mtime               ctime
    ## /etc 3638  TRUE  755 2015-07-22 10:51:58 2015-07-22 10:51:58
    ##                    atime uid gid uname grname
    ## /etc 2015-08-01 08:11:43   0   0  root  wheel
    
    file.info("/etc/hosts")
    ##              size isdir mode               mtime               ctime
    ## /etc/hosts 787448 FALSE  644 2015-07-19 17:34:59 2015-07-19 17:34:59
    ##                          atime uid gid uname grname
    ## /etc/hosts 2015-08-06 12:30:26   0   0  root  wheel
    
    file_test("-f", "/etc/hosts") # is a file and not a dir
    ## [1] TRUE
    
    file_test("-d", "/etc") # is a dir
    ## [1] TRUE
    
    dir.exists("/etc") # R 3.2.0+
    ## [1] TRUE
    
    file.exists("/etc/hosts")
    ## [1] TRUE
    
    Sys.readlink("/etc") # will return something if it's a symbolic link
    ## [1] "private/etc"
    
    Sys.readlink("/tmp")
    ## [1] "private/tmp"
    
    Sys.readlink("/bin") # or "" if not
    ## [1] ""
    

    【讨论】:

    • 恭喜@hrbrmstr,你击败了谷歌!你知道没人知道的事!无论如何,非常感谢你,这对我帮助很大。
    猜你喜欢
    • 2018-12-18
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2011-05-31
    • 2011-12-26
    • 2013-08-14
    相关资源
    最近更新 更多