frank.am
frank.am

Reputation: 47

Remove filename from URLs

I need to remove just filename (Ex.: 'number.pdf') from a URL:

  1. http://URL.com/S01/000/users/info/4512.pdf
  2. http://URL.com/S0152/01009/users/old/info/007401.pdf
  3. http://URL.com/S45012/0000/users/info/14.pdf
  4. http://URL.com/S450101/0/users/new/info/7895409.pdf
  5. http://URL.com/S01/0000/users/info/1045.pdf
  6. http://URL.com/S004/9082-00/users/areas-latam-co/info/65471.pdf
  7. http://URL.com/S004/9082-246/users/areas-us/info/1530.pdf

Finally, I'd like get just this:

  1. http://URL.com/S01/000/users/info/
  2. http://URL.com/S0152/01009/users/old/info/
  3. http://URL.com/S45012/0000/users/info/
  4. http://URL.com/S450101/0/users/new/info/
  5. http://URL.com/S01/0000/users/info/
  6. http://URL.com/S004/9082-00/users/areas-latam-co/info/
  7. http://URL.com/S004/9082-246/users/areas-us/info/

How Can i do it?

Any idea? Who say I?

Upvotes: 2

Views: 1065

Answers (1)

Aaron Brock
Aaron Brock

Reputation: 4536

This can be easily done with os.path.dirname().

Example:

import os

print(os.path.dirname('http://URL.com/S01/000/users/info/4512.pdf'))

Output:

http://URL.com/S01/000/users/info

Upvotes: 3

Related Questions