gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:21:30 27 4
gpt4 key购买 nike

在 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/

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