java如果目录不存在则创建目录_检查目录是否存在,如果不存在则创建

我经常发现自己写的R脚本会产生大量输出。 我发现它更干净,可以将此输出放到自己的目录中。 我在下面编写的内容将检查目录是否存在并移入该目录,或者创建目录然后移入该目录。 有没有更好的方法来解决这个问题?

mainDir

subDir

if (file.exists(subDir)){

setwd(file.path(mainDir, subDir))

} else {

dir.create(file.path(mainDir, subDir))

setwd(file.path(mainDir, subDir))

}

#1楼

就一般体系结构而言,我建议在目录创建方面采用以下结构。 这将涵盖大多数潜在的问题,并且dir.create调用将检测到目录创建的其他任何问题。

mainDir

subDir

if (file.exists(paste(mainDir, subDir, "/", sep = "/", collapse = "/"))) {

cat("subDir exists in mainDir and is a directory")

} else if (file.exists(paste(mainDir, subDir, sep = "/", collapse = "/"))) {

cat("subDir exists in mainDir but is a file")

# you will probably want to handle this separately

} else {

cat("subDir does not exist in mainDir - creating")

dir.create(file.path(mainDir, subDir))

}

if (file.exists(paste(mainDir, subDir, "/", sep = "/", collapse = "/"))) {

# By this point, the directory either existed or has been successfully created

setwd(file.path(mainDir, subDir))

} else {

cat("subDir does not exist")

# Handle this error as appropriate

}

还要注意,如果~/foo不存在,则除非指定recursive = TRUE否则对dir.create('~/foo/bar') recursive = TRUE将失败。

#2楼

要找出路径是否为有效目录,请尝试:

file.info(cacheDir)[1,"isdir"]

file.info不在意最后的斜线。

如果目录中以斜杠结尾,则Windows上的file.exists将失败,如果没有该目录,则成功。 因此,这不能用于确定路径是否为目录。

file.exists("R:/data/CCAM/CCAMC160b_echam5_A2-ct-uf.-5t05N.190to240E_level1000/cache/")

[1] FALSE

file.exists("R:/data/CCAM/CCAMC160b_echam5_A2-ct-uf.-5t05N.190to240E_level1000/cache")

[1] TRUE

file.info(cacheDir)["isdir"]

#3楼

从2015年4月16日开始,随着R 3.2.0的发布,有一个名为dir.exists()的新函数。 要使用此功能并创建目录(如果目录不存在),可以使用:

ifelse(!dir.exists(file.path(mainDir, subDir)), dir.create(file.path(mainDir, subDir)), FALSE)

如果目录已经存在或无法创建,则返回FALSE如果目录不存在但创建成功,则返回TRUE 。

请注意,只需检查目录是否存在,即可使用

dir.exists(file.path(mainDir, subDir))

#4楼

在原始文章中,使用file.exists()来测试目录是否存在是一个问题。 如果subDir包含现有文件的名称(而不只是路径),file.exists()将返回TRUE,但是对setwd()的调用将失败,因为您无法将工作目录设置为指向文件。

我建议使用file_test(op =“-d”,subDir),如果subDir是现有目录,它将返回“ TRUE”,但是如果subDir是现有文件或不存在的文件或目录,则返回FALSE。 同样,可以使用op =“-f”完成文件检查。

此外,如另一条评论中所述,工作目录是R环境的一部分,应由用户而不是脚本控制。 理想情况下,脚本不应更改R环境。 为了解决这个问题,我可以使用options()将全局存储的目录存储在我想要所有输出的位置。

因此,请考虑以下解决方案,其中someUniqueTag只是选项名称的程序员定义的前缀,这使得不太可能已经存在具有相同名称的选项。 (例如,如果要开发一个名为“ filer”的软件包,则可以使用filer.mainDir和filer.subDir)。

以下代码将用于设置以后可在其他脚本中使用的选项(从而避免在脚本中使用setwd()),并在必要时创建文件夹:

mainDir = "c:/path/to/main/dir"

subDir = "outputDirectory"

options(someUniqueTag.mainDir = mainDir)

options(someUniqueTag.subDir = "subDir")

if (!file_test("-d", file.path(mainDir, subDir)){

if(file_test("-f", file.path(mainDir, subDir)) {

stop("Path can't be created because a file with that name already exists.")

} else {

dir.create(file.path(mainDir, subDir))

}

}

然后,在需要在subDir中操作文件的任何后续脚本中,都可以使用类似以下内容的代码:

mainDir = getOption(someUniqueTag.mainDir)

subDir = getOption(someUniqueTag.subDir)

filename = "fileToBeCreated.txt"

file.create(file.path(mainDir, subDir, filename))

该解决方案将工作目录置于用户的控制之下。

#5楼

我遇到了R 2.15.3的问题,当尝试在共享网络驱动器上递归创建树结构时,会出现权限错误。

为了避免这种怪异,我手动创建了结构。

mkdirs

if(!file.exists(fp)) {

mkdirs(dirname(fp))

dir.create(fp)

}

}

mkdirs("H:/foo/bar")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值