r - Windows 中的长路径/文件名会使 R 中的 write.table() 出错

标签 r long-filenames

在 R 中,我使用 write.table() 将文件写入嵌入长名称目录中的位置。但出现如下错误:

Error in file(file, ifelse(append, "a", "w")) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, "a", "w")) : cannot open file 'data/production/Weekly_Prod_201407_Selling_Price_Snapshot_20140930_Median_Selling_Price_Map.csv': No such file or directory

然后,当我将文件名缩短为 Weekly_Prod.csv 时,它成功了!所以看来长路径和长文件名导致 R 出错。

我测试了几次,发现路径+文件名的总长度限制为260个字符。也就是说,当 R 达到 261 个字符或更多时,就会出错。有办法解决这个问题吗?请帮忙。谢谢!

最佳答案

Windows 上的文件路径长度有限制:

> write(1, paste0(paste(sample(letters, 150, TRUE), collapse = ''), '.txt'))
> write(1, paste0(paste(sample(letters, 250, TRUE), collapse = ''), '.txt'))
Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file 'qvxirpnlwkqfwlxhggkscxlwhhyblrwxfpikpsukrfqwhaqvsyhdpihnoknqmxgafvawxkuijqbmvgdjwwgeumfksmhtiqwvzwmjukmmmeesvcdpdbpimarxssnrngfxwjksqshjruralhtwdnfmdhzrcwcdrnwezdhwqyisbjikdhbbygtcoeechgwrewenewbrlexliiikdnwlclbzllaxcohacadxzztgmtnmppyxtxtbopxdokjnvx.txt': No such file or directory

根据此来源,它是 260 个字符

http://msdn.microsoft.com/en-us/library/aa365247.aspx#maxpath

> nchar(getwd())
[1] 23
> write(1, paste0(paste(sample(letters, 231, TRUE), collapse = ''), '.txt'))
> write(1, paste0(paste(sample(letters, 232, TRUE), collapse = ''), '.txt'))
Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file 'topylmudgfnrkdilqbklylwtbwrgwbwmamxzhwwzlxxslqeuhpywahoxqxpkckvmkfjccbsqncctlovcnxctkyvgunnbqcwyiliwpfkjibanpmtupsxfboxnjaadovtdpxeloqjnbqgvkcilwljfswzlrlqixmwqpoemcemhdizwwwbgqruhepyrskiklkbylzjhrcchbusohkrwyzgablvngqrqiardubcbziex.txt': No such file or directory
> getwd()
[1] "C:/Users/john/Documents"

> nchar(file.path(getwd(), paste0(paste(sample(letters, 231, TRUE), collapse = ''), '.txt')))
[1] 259

一种可能适合您的解决方案是为您的长目录路径创建一个虚拟驱动器。它应该给你一点回旋余地,请参阅 https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/subst.mspx?mfr=true

> system("subst x: C:/Users/john/Documents")
> write(1, paste0("x://", paste(sample(letters, 251, TRUE), collapse = ''), '.txt'))

使用完虚拟驱动器后,您可以使用以下命令进行反转:

system("subst x: /D")

关于r - Windows 中的长路径/文件名会使 R 中的 write.table() 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26151365/

相关文章:

r - ggplot2 (Barplot + LinePlot) - 双 Y 轴

r - 如何将标签放置在远离饼图的位置

c# - 如何在 SaveFileDialog 的默认文件名中设置长字符串(> 260)?

r - 如何删除部分 y 轴并反转 ggplot2 中的轴

python - 理解梯度提升回归树的部分依赖

r - 为什么在 tidyr::pivot_wider 中尝试使用 names_glue 时出现错误?

encryption - encfs 中的文件名长度限制是否有解决方法?

windows - 是否可以使用特殊字符来表示 Windows 中的空格?

assembly - 在 16 位程序集中创建具有长文件名的文件