问题
In R, I was using write.table()
to write a file into a location embedded in directories with long names. But it errors out as below:
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
Then when I shortened the filename to Weekly_Prod.csv
, it worked! So it seems the long path and the long filename caused R to error out.
I tested it a few times and found that the limit is 260 characters for the total length of path+filename. That is, R errors out when it's 261 characters or more. Is there a way to get around of this? Please help. Thanks!
回答1:
There is a limit on file path length on 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
According to this source it is 260 characters
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
One possible solution which may work for you is to create a virtual drive for your long directory path. It should give you a bit of leeway see 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'))
when you are done with the virtual drive you can reverse using:
system("subst x: /D")
回答2:
This could be taken care of by replacing the name of the said file with its Short File Name (SFN), also known as the 8.3 file name.
Type dir /x in the command prompt over the directory where the file is located, which would list all the SFN's of the files in the directory.
Then replace the file-name in your code with its corresponding 8.3 file-name.
来源:https://stackoverflow.com/questions/26151365/long-path-filename-in-windows-makes-write-table-error-out-in-r