A solution to the problem that files containing [and] are not listed in glob.glob ()

I stumbled on glob.glob (), so make a note.

phenomenon

With glob.glob (), you cannot get the path containing [].

Example

glob.glob(r"d:\test\[1].*") 
#=> [1].Does not match txt
glob.glob(r"d:\test\\[1\].*") # \[ \]You can't write

Solution

  1. Escape the path mask as follows
[ -> [[]
] -> []]
glob.glob(r"d:\test\[[]1[]].*") 
#=> [1].Matches txt

You can also use the following function instead of glob.glob ().

def escapeBraceForGlob(str):
    '''
    convert [ -> [[]  ,  ] -> []]
    '''
    newStr = str.replace("[","\\[").replace("]","\\]")
    newStr = newStr.replace("\\[","[[]").replace("\\]","[]]")
    return newStr


def globEscapeBraces(pathname):
    '''
    glob.glob() after escaping "[" and "]".
    '''
    return glob.glob(escapeBraceForGlob(pathname))
  1. Use os.listdir (). However, note that even the files in the subdirectories are listed.

reference

Recommended Posts

A solution to the problem that files containing [and] are not listed in glob.glob ()
How to get the files in the [Python] folder
A solution to the problem that files containing [and] are not listed in glob.glob ()
How to download files from Selenium in Python in Chrome
A solution to the problem that the Python version in Conda cannot be changed
Upload and manage packages that are not in conda to anaconda.org
Solves the problem that static files (CSS, JS, img) are not read when DEBUG = False in Django.
The NVM Checksum Is Not Valid, a solution to the problem that Intel's wired LAN is not recognized on Linux
Solution to the problem that build does not end when installing OpenCV (PEP517)
Solution to the problem that you can't activate by putting conda in pyenv
Try to extract the keywords that are popular in COTOHA
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??
Finding a solution to the N-Queen problem with a genetic algorithm (1)
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
[VLC] How to deal with the problem that it is not in the foreground during playback
Solution to the problem that the display is corrupted when the .exe command is included in the while loop in wsl2
a () and a.__ call__ () are not equivalent
Try to write a program that abuses the program and sends 100 emails
Bug that unnecessary files are created when the -i and -e options are added to the sed command.
"The Cathedral and the Bazaar" that only those who work in a solid company want to read
Solution to the problem that Ctrl + z cannot be used in Powershell in Docker for windows environment (provisional)
Convert files uploaded to Cloud Storage with Cloud Functions (Python) so that they are not garbled in Excel
Function to extract the maximum and minimum values ​​in a slice with Go
I want to visualize where and how many people are in the factory
How to count the number of elements in Django and output to a template
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
I want to see a list of WebDAV files in the Requests module
Examples and solutions that the Python version specified in pyenv does not run
A tool to insert the country name and country code in the IP address part
How to get a list of files in the same directory with python
[Python] Get the files in a folder with Python
Try to calculate a statistical problem in Python
The return value (generator) of a function that combines finally and yield must not be passed directly to next
How to get the files in the [Python] folder
A solution to the problem that kernel restarting frequently occurs when running PyQt system with jupyter or Spyder IDE
A Python script that crawls RSS in Azure Status and posts it to Hipchat
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
Note that I was addicted to npm script not passing in the verification environment
[systemd] How to deal with the problem that fancontrol does not work after suspending
How to use a library that is not originally included in Google App Engine
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
plotly trace and layout templates that are likely to be used in scatter plots
[pyqtgraph] Created a class to display the crosshairs that follow the cursor and their coordinates
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
Convert a path string that uses a symbolic link in the middle to an absolute path
How to unit test a function containing the current time using freezegun in python
Solve the problem that CSS is not reflected during web application development in Flask
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
Qiita article summary that solved the Stack Overflow problem that always sets (a == 1 && a == 2 && a == 3) to true
[Mac] A super-easy way to execute system commands in Python and output the results
A script that transfers tweets containing specific Twitter keywords to Slack in real time
A note that runs an external program in Python and parses the resulting line
[Memorandum] ① Get and save tweets ~ I want to identify the news tweets that are spread ~
How to find the first element that matches your criteria in a Python list
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
To extract the data of a specific column in a specific sheet in multiple Excel files at once and put the data in each column in one row
Do not pass self to ProcessPoolExecutor in the class
Output a binary dump in binary and revert to a binary file
How to reference static files in a Django project
2 ways to read all csv files in a folder
The story that yapf did not work in vscode
A server that returns the number of people in front of the camera with bottle.py and OpenCV