I want to do the following search with python 3.x.
For example, when the following five files exist, I want to search for a file that does not contain the letter "b" and contains "a".
file = glob.glob ('* a.txt'), you can get a file path other than c.txt, but what I want to achieve is the two ID1_a.txt and ID2_a.txt Get the file.
Answer # 1
Although often misunderstood,
The glob argument can be a string containing awildcard, not a regular expression.
Even if you don't bring up a regular expression, you can check the attribution with the in operator.
It is unverified, but like this.
filenames =  for filename in glob.iglob ('*'): if 'b' in filename: continue if 'a' in filename: filenames.append (filename)
You can use list comprehensions.
filenames = [ filename for filename in glob.iglob ('*') if 'b' not in filename and 'a' in filename ]
glob.iglob is used from the viewpoint of saving memory, but there is no problem with glob.glob.
Answer # 2
#-*-coding: utf-8-*- from glob import iglob for filename in iglob ('ID * _a.txt'): print (filename)
Not like this?
Answer # 3
I wonder if you should use "post-reading statement".
a = ["ID1_a.txt", "ID1_ba.txt", "ID2_a.txt", "ID2_ba.txt", "c.txt"] b = [x for x in a if re.search (r "(?<! b) a.txt", x)] print (b)
- python - beautifulsoup with a specific string
- python - i want to judge and extract the character string of a specific column by pandas with a numerical value
- python - i want to extract the character string excluding the blank characters at the end of the line
- python - discord's bot will send emoticons to specific text channels when there are more than 3 people online
- i want to detect an empty string with java regular expression
- python 3x list distinguish character at specific position
- about regular expressions including python html tags
- python - i want to change the specific part of the excel file converted by openpyxl from a character string to a number
- python - i want to get a specific attribute value from multiple existing tags
- how to print a string with class specified in for in python beautifulsoup
- python - cannot extract the character string
- [python] regular expression to check if there is a line break after the
- python - i want to extract a url consisting of specific characters ~regular expressions~
- How to convert string case in Python
- how to use string format for% operator in python 3
- python - i want to color specific days in django html
- python - about the problem that the character string sent from the pc by serial communication cannot be recognized on the arduin
- i want to replace the string in the file with sed, a regular expression
- Specific use of playbook of Ansible module in python
- i want to get the string with python selenium
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python 3x - import error "cannot import name '_imaging'"