4

I have the following data:

/​share/​Downloads/​Videos/​Movies/​Big.Buck.Bunny.​720p.​Bluray.​x264-BLA.​torrent/Big.Buck.Bunny.​720p.​Bluray.​x264-BLA

However, I dont want to have "Big.Buck.Bunny.​720p.​Bluray.​x264-BLA.torrent/" in it, I want the path to be like:

/​share/​Downloads/​Videos/​Movies/Big.Buck.Bunny.​720p.​Bluray.​x264-BLA

With regular expressions I basically want to math anything that holds *.torrent./, how can I accomplish this in regexp?

Thanks!

3
  • You could move it in the file structure, or rename the parent folder. That's always easier. Commented Mar 11, 2012 at 18:39
  • We're not talking about making changes to the filesystem, this is for a different purpose
    – FLX
    Commented Mar 11, 2012 at 18:42
  • 2
    Not really, my job requires me to deal with legal torrents, such as this free movie.
    – FLX
    Commented Mar 11, 2012 at 20:43

5 Answers 5

16

You don't even need regular expressions. You can use os.path.dirname and os.path.basename:

os.path.join(os.path.dirname(os.path.dirname(path)),
             os.path.basename(path))

where path is the original path to the file.

Alternatively, you can also use os.path.split as follows:

dirname, filename = os.path.split(path)
os.path.join(os.path.dirname(dirname), filename)

Note This will work under the assumption that what you want to remove is the directory name that contains the file from the path as in the example in the question.

1
  • 1
    +1 for giving the best (most robust) way to solve the problem at hand. Commented Mar 11, 2012 at 19:18
4

You can do this without using regexp:

>>> x = unicode('/share/Downloads/Videos/Movies/Big.Buck.Bunny.720p.Bluray.x264-BLA.torrent/Big.Buck.Bunny.720p.Bluray.x264-BLA')
>>> x.rfind('.torrent')
66
>>> x[:x.rfind('.torrent')]
u'/share/Downloads/Videos/Movies/Big.Buck.Bunny.720p.Bluray.x264-BLA'
1
  • Thanks but I'm looking for a regex way :)
    – FLX
    Commented Mar 11, 2012 at 18:52
1

I basically want to math anything that holds *.torrent./, how can I accomplish this in regexp?

You can use:

[^/]*\.torrent/

Assuming the last . was a typo.

2
  • Thanks! I just tried this in gskinner.com/RegExr but its not working unfortunately :(
    – FLX
    Commented Mar 11, 2012 at 18:52
  • @FLX, it works just fine: regexr.com?309kb Replace the match with empty string to get the result you are looking for. (Note that your pasted path has some "hidden" characters that mess things up, at least for me.)
    – Qtax
    Commented Mar 11, 2012 at 18:57
1

Given path='/share/Downloads/Videos/Movies/Big.Buck.Bunny.720p.Bluray.x264-BLA.torrent/Big.Buck.Bunny.720p.Bluray.x264-BLA'

You can do it with regular expression as

re.sub("/[^/]*\.torrent/","",path)

You can also do it without regex as

'/'.join(x for x in path.split("/") if x.find("torrent") == -1)
0

Your question is a bit vague and unclear, but here's one way how to strip off what you want:

import re
s = "/share/Downloads/Videos/Movies/Big.Buck.Bunny.720p.Bluray.x264-BLA.torrent/Big.Buck.Bunny.720p.Bluray.x264-BLA"

c = re.compile("(/.*/).*?torrent/(.*)")
m = re.match(c, s)
path = m.group(1)
file = m.group(2)
print path + file

>>> ## working on region in file /usr/tmp/python-215357Ay...
/share/Downloads/Videos/Movies/Big.Buck.Bunny.720p.Bluray.x264-BLA

Not the answer you're looking for? Browse other questions tagged or ask your own question.