Skip to content

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

@vanatteveldt

Description

@vanatteveldt

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions