0

Here is an example of what i need.

Suppose that we have the following string:

str = "/home/user/folder/MyVeryLongFileName.foo"

I have multiple operations to do on this one :

  1. remove the path (assuming i have its length) :

    str = str[path_length:]
    
  2. revome the extension (always 4 char in my case) :

    str = str[path_length:-4]
    

    So, right now my string looks like MyVeryLongFileName

  3. Now I would like to limit its size at 15 characters. Is it possible to do it in the same expression ? Or may I have to do it after the 2 previous operations ?

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Simon PA
  • 748
  • 13
  • 26

3 Answers3

2

If you want only the first 15 characters, then you can slice the string again, like this:

file_name[path_length:-4][:15]

If you really are dealing with filenames, you might want to go with

>>> file_name = "/home/user/folder/MyVeryLongFileName.foo"
>>> import os
>>> print os.path.split(file_name)[1].rpartition(".")[0][:15]
MyVeryLongFileN

Or:

>>> print os.path.basename(file_name).rpartition(".")[0][:15]
'MyVeryLongFileN'

Also, it would be better to use splitext to get the extension, like this

>>> from os.path import basename, splitext
>>> print splitext(basename(file_name))[0][:15]
MyVeryLongFileN
anon582847382
  • 19,907
  • 5
  • 54
  • 57
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

You can get the filename with this:

>>> print str.split('/')[-1]
MyVeryLongFileName.foo

Remove the extension with:

>>> print str.split('.')[0]
/home/user/folder/MyVeryLongFileName

Limit the file name to 15 characters:

>>> print str.split('/')[-1][:15]
MyVeryLongFileN

This being said, you can always use the bash utils to extract this info. basename is the tool to get the file and dirname to get the path. See Extract filename and extension in bash for more info.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I don't get why you suggest to use bash's `basename` and `dirname` (when the OP never mentioned bash nor tagged the question with the tag) when your python code doesn't use them (since they are available as `os.path.basename`, `os.path.dirname`). Your answer is certainly inferior to all other answers to this question and it's suggesting a *bad* solution to the problem (because it's OS specific and fails in certain situations). – Bakuriu Apr 11 '14 at 19:04
  • @Bakuriu well I intentionally placed a
    above the extra information, because it was a different approach. I agree there can be other ways, even better, but having a path with normal slashes `/` gives idea that we are in a *NIX environment. Feel free to suggest improvements so that we can all learn.
    – fedorqui Apr 11 '14 at 19:48
1

I would do this:

>>> from os.path import splitext, basename
>>> apath = "/home/user/folder/MyVeryLongFileName.foo"
>>> splitext(basename(apath))[0][:15]
'MyVeryLongFileN'

splitext separates the file-extension from the rest, and we do this on the result of basename which splits the part into the base file-name and the rest of the path. Then we can cut down the remaining string. I would definitely use these methods because they are much more reliable.

anon582847382
  • 19,907
  • 5
  • 54
  • 57