11

I have a string which is basically a file path of an .mp4 file.

I want to test if the file path is matching one of the following patterns:

/*.mp4   (nothing before the slash, anything after)
*/*.mp4  (anything before and after the slash)
[!A]*.mp4  (anything before the extension, **except** for the character 'A')

What would be the best way to achieve this? Thanks!

EDIT:

I'm not looking to test if the file ends with .mp4, i'm looking to test if it ends with it and matches each of those 3 scenarios separately.

I tried using the 'endswith' but it's too general and can't "get specific" like what i'm looking for in my examples.

wim
  • 338,267
  • 99
  • 616
  • 750
Gambit2007
  • 3,260
  • 13
  • 46
  • 86
  • 3
    `string.endswith('mp4')`? – castis Jun 03 '16 at 15:29
  • that's a generalization, i'm looking for a way to test _each_ of the above scenarios separately. – Gambit2007 Jun 03 '16 at 15:29
  • what's your os? have you tested anything? files = os.listdir('.') will give you the files in your cwd, and you can test your first paramter with 'for f in files: if file.endswith('.mp4') "do stuff" (you can also satisfy your third paramter follwoing that with 'if 'A' not in f' – st.ph.n Jun 03 '16 at 15:30
  • what's up with all of the down votes? 'endswith' doesn't work with star/star.mp4 or any of the other examples i provided – Gambit2007 Jun 03 '16 at 15:35
  • 1
    Read the docs for [```re```](https://docs.python.org/3/library/re.html), try your luck with an [online regex tester](https://www.google.com/search?q=python+regex+tester&oq=python+regex&aqs=chrome.2.69i57j0l2j69i65j0l2.7249j0j7&sourceid=chrome&ie=UTF-8), write some code. – wwii Jun 03 '16 at 15:35
  • Well you just started the question the wrong way I guess – gerosalesc Jun 03 '16 at 15:36
  • 3
    @Gambit2007 The question does not show any research effort. See [ask] – Bhargav Rao Jun 03 '16 at 15:37
  • i don't know it seems pretty clear to me.. – Gambit2007 Jun 03 '16 at 15:37
  • @Gambit2007 see Bhargav comment – gerosalesc Jun 03 '16 at 15:38
  • 1
    alright, it really was my bad. yet people still marked this question as a duplicate when it wasn't. – Gambit2007 Jun 03 '16 at 15:41
  • Yeah you're supposed to look up solutions, try them and let usbknowbwhat went wrong. The question didn't really demonstrate that. But that being said, it was useful for me to look up the different scenarios and try to find solutions. – EoinS Jun 03 '16 at 15:42

1 Answers1

19

Here they are:

string.endswith('.mp4') and string.startswith('/')

string.endswith('.mp4') and "/" in string

string.endswith('.mp4') and "A" not in string

Or, look at using fnmatch.

wim
  • 338,267
  • 99
  • 616
  • 750
EoinS
  • 5,405
  • 1
  • 19
  • 32
  • just one correction - in order to find a forwardslash you need to escape it so it would actually be '//' – Gambit2007 Jun 03 '16 at 15:50
  • 2
    @Gambit2007 no, you wouldn't need to escape it. On most systems, only backslashes must be escaped. – Jules Jun 03 '16 at 16:17
  • Okay, i'm using OS X El Capitan and i had to escape it – Gambit2007 Jun 03 '16 at 16:25
  • @wim why did you change 'A' not in string to not string.startswith('a') . Seems different to third criteria. Also, why fnmatch instead of find – EoinS Nov 07 '18 at 17:03
  • 1
    My mistake. Changed it back. `fnmatch` is the usual approach to this kind of globbing, it might help other readers to have it mentioned. find is no good here because that doesn't support wildcarding in any way. – wim Nov 07 '18 at 17:09