Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable globbing? Error on reading file with square brackets in file name #169

Open
vanatteveldt opened this issue Jun 21, 2022 · 0 comments

Comments

@vanatteveldt
Copy link

I encountered an error when reading in a (text) file with [1] in the name:

> readr::write_file("test", file="test[1].txt")
> readtext::readtext("test[1].txt")
Error in list_files(file, ignore_missing, TRUE, verbosity) : 
  File '' does not exist.

This is presumably caused by globbing, i.e. it treats [1] as a character class.

Is there a way to disable globbing? In this case, I have a list of files that I want to read, and I want to treat the file list as a list of literal file names, not globbing patterns.

Note that
(1) It actually took me a while to figure out globbing was the problem, as I associate globbing with *, not with [], and the error message is also not very helpful since the globbed file pattern becomes an empty string, so I had to figure out which of my (300k) files was causing the problem;
(2) how globbing works is system dependent (as per the Sys.glob documentation), which says that a glob system call is used which "probably" includes character classes
(3) it is unclear to me how to escape globbing patterns, at least using text\\[1\\].txt did not work, even though Sys.glob("test\\[1\\].txt") works as expected, so apparently something happens in between.

Reproducible case and failed attempts at escaping the glob:

> readr::write_file("test", file="test[1].txt")
> readtext::readtext("text[1].txt")
Error in list_files(file, ignore_missing, TRUE, verbosity) : 
  File '' does not exist.
> readtext::readtext("text\\[1\\].txt")
Error in list_files(file, ignore_missing, TRUE, verbosity) : 
  File '' does not exist.
> readtext::readtext("text\\[1].txt")
Error in list_files(file, ignore_missing, TRUE, verbosity) : 
  File '' does not exist.
> readtext::readtext("text\\\\[1\\\\].txt")
Error in list_files(file, ignore_missing, TRUE, verbosity) : 
  File '' does not exist.
> readtext::readtext("text\[1\].txt")
Error: '\[' is an unrecognized escape in character string starting ""text\["
> Sys.glob("test[1].txt")
character(0)
> Sys.glob("test\\[1\\].txt")
[1] "test[1].txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant