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 - i want to judge and extract the character string of a specific column by pandas with a numerical value
- python - beautifulsoup with a specific string
- python 3x - i want to search for a specific string in the list and return the specified value [python]
- [python] regular expression: about the output of finder()
- i want to judge and extract a line containing a specific character string from a python dataframe
- python - how to assign two functions to a string
- extract specific rows from python csv data
- python - how to get data from model class by specifying specific value from query set
- excel - a string operation in a specific column of a dataframe
- python - discordpy permission settings for specific channels
- python - i want to replace only the exact string
- python3 string array combination code creation
- python - django noreversematch occurs only on a specific page
- python 3x - python3 string processing
- awk replaces a specific string and a string within the range enclosed by the string
- i want to repeatedly extract a string after a specific string for a regular expression in python
- awk comment out a specific character string and add the character string
- python - how to extract a color other than a specific color that is opencv
- python - how to select a specific number by default at startup in treeview of tkinter
- extract specific line in python
- 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
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- android studio - unresolved reference comes out in kotlin
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- python - importerror: cannot import name md5 error cannot be resolved