from re import * v=split(r"[\s,)(]", r"(C:\Users\Gleb\Desktop\сайты.txt,C:\Users\Gleb\Desktop\istock_000020116885large_-_copy-700x461.jpg)") print(v) 

The code displays:

['', 'C:\Users\Gleb\Desktop\сайты.txt', 'C:\Users\Gleb\Desktop\istock_000020116885large_-_copy-700x461.jpg', '']

And should display:

['C:\Users\Gleb\Desktop\сайты.txt', 'C:\Users\Gleb\Desktop\istock_000020116885large_-_copy-700x461.jpg']

Why so deduces?

  • strip() initially apply to a string or something. - Visman

2 answers 2

You cut by given literals, this is exactly what a python does , on the one hand, the literal C , and on the other? Nothing - an empty string, so he inserts it.

To get what you need, you must tell the python what you want to get , not what you don't need .

In the comments, @ gil9red suggested a more regular expression:
r"\w.+?\.\w+"

 import re string = r"(C:\Users\Gleb\Desktop\сайты.txt,C:\Users\Gleb\Desktop\istock_000020116885large_-_copy-700x461.jpg)" regex = r"([\w\:\\\.\-]+)" print(re.findall(regex, string)) 

Living example

  • I have such a regular result: r"\w:.+?\.\w+" . Example: regex101.com/r/PXSQx3/1 - gil9red
  • @ gil9red, yours is even better, with a small reservation "\ w. +? \. \ w +" you do not need a colon. I will add it in response. - Vasiliy Rusin
  • The colon pointed to the drive letter C: D: - gil9red
  • @ gil9red you already specify .+? . The colon here is redundant, and in any case you do not screen it, it does not do what you expect. - Vasiliy Rusin
  • Regularly, I wrote guided by the fact that I want to find the file paths from beginning to end, the beginning is the drive letter ( \w: , then I’m not looking for all the characters ( .+? ) To the end of the file, by its suffix ( \.\w+ ). Therefore, I do not think that there is redundancy. It is clear that such a suffix can be found in the name of the folder, but there should be restrictions. In general, regarding redundancy, the same result can be achieved even easier. I will show in response - gil9red

In simple text processing cases, you can do without regulars:

 text = r"(C:\Users\Gleb\Desktop\сайты.txt,C:\Users\Gleb\Desktop\istock_000020116885large_-_copy-700x461.jpg)" filename_list = text[1:-1].split(',') print(filename_list) 

Result:

 ['C:\\Users\\Gleb\\Desktop\\сайты.txt', 'C:\\Users\\Gleb\\Desktop\\istock_000020116885large_-_copy-700x461.jpg']