-1

I have a file that contains a bunch of filenames, i.e.:

hello.txt  
goodbye.py  
test..pdf  
n3w.world.file.text
...

I am trying to ignore all filenames with multiple dots. Currently I am able to find all the filenames using:

import re
data = ['hello.txt', 'goodbye.py', 'test..pdf', 'n3w.world.file.text']
matches = re.findall('([\w].+)\.(\w+)', data)
print(matches)

However this prints out all the filenames. How can I modify this to just print out: hello.txt and goodbye.py?

wovano
  • 4,543
  • 5
  • 22
  • 49
shady mccoy
  • 125
  • 1
  • 9
  • 1
    Does this answer your question? [Count the number of occurrences of a character in a string](https://stackoverflow.com/questions/1155617/count-the-number-of-occurrences-of-a-character-in-a-string) – Tomerikoo Jul 26 '21 at 07:24

2 Answers2

3

Regex in this case is overkill. You can use str.count() function:

data = ['hello.txt', 'goodbye.py', 'test..pdf', 'n3w.world.file.text']

data = [w for w in data if w.count('.') < 2]
print(data)

Prints:

['hello.txt', 'goodbye.py']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
-2
   data = [i for i in data if '..' not in i]

You can use the above code

Vikika
  • 318
  • 1
  • 9
  • 1
    This *would* be a great answer if the question was to ignore filenames with multiple *consecutive* dots. However, it doesn't mention that, and the provided example data indicates otherwise: your solution also includes 'n3w.world.file.text', which is not correct according to the criteria in the question. So, the solution of Andrej is more accurate. – wovano Jun 11 '19 at 17:04