2

I want to look for the extension of a filename, and the extension can only be 3 characters long.

it is something like,

filename = str(input("Please enter filename: "))

then I want to execute a task to look for the extension, and if the extension doesn't fulfill the requirement (i.e. 3 characters long), I'll add print "Invalid extension!" Else, print "Valid extension."

Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
DarsAE
  • 185
  • 3
  • 4
  • 12
  • 1
    How can you check if the extension is valid or not, what if the filename is `a....t#t`, you can't tell if extension is valid only by checking for 3 characters length of extension – avasal Mar 09 '12 at 11:33
  • Why do you need the file name's extension to be "valid" in this sense? – Karl Knechtel Mar 09 '12 at 11:39
  • I too am curious about this extension checking - @Daria, is this a specific application that requires a specific extension length, or are you assuming that all files are supposed to have an extension and that it should be a particular size? – David Mason Mar 10 '12 at 04:22

3 Answers3

7
len(os.path.splitext('/foo/file.png')[1]) - 1 == 3
Diego Navarro
  • 9,316
  • 3
  • 26
  • 33
  • As alluded to in avasal's comment, once you have the extension you might want also want to check other things about it, like whether it contains characters that you don't want. – David Mason Mar 10 '12 at 04:20
  • 1
    +1 for using the standard library instead of rolling your own (inevitably incomplete) version of `os.path.splitext()`. – Li-aung Yip Mar 27 '12 at 06:09
0

You can "cut" it like that if the extension is always 3 caracters :

$ python2
Python 2.7.2 (default, Jan 31 2012, 13:19:49) 
[GCC 4.6.2 20120120 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "file.ext"
>>> print x[-3:]
ext

See Extracting extension from filename in Python too

Edit: You can rely on the mime type instead, depending of your needs, see How to find the mime type of a file in python?

Community
  • 1
  • 1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    This is a good one, but what if the file is "file.conf"? it will return `onf` and it will be 3 chars long too – Diego Navarro Mar 09 '12 at 11:26
  • Yes, that's why I point you to another links. Diego Navarro's snippet can be use to test your extention length too ;) – Gilles Quénot Mar 09 '12 at 11:30
  • This does not help with the question at all, since there is a prompt and the extension must be checked programatically, not by visual inspection. In addition, this can return 3 characters even if there is no extension at all. – David Mason Mar 10 '12 at 04:14
0

This should do what you are describing:

filename = raw_input("Please enter filename: ") #This is already a string
if len(filename.split('.')[-1])==3:
   print "Valid extension"
else:
   print "Invalid extension!"

Hope this helps.

dtrujillo
  • 553
  • 6
  • 10
  • For '/path/my.file.ext' this will return 'file'. Using index [-1] to get the last part would be more correct, but it will still return the whole thing if there is no extension (e.g. using '/path/myfile' you will get '/path/myfile', or using '/os' it will give you '/os' which has length 3 and would appear valid. – David Mason Mar 10 '12 at 04:17
  • You're very right, I didn't take into account inputs without a dot or with multiple dots. Thanks for pointing it out. – dtrujillo Mar 10 '12 at 13:20
  • I used this way and turned out that it works! Thank you guys for being so helpful x – DarsAE Mar 20 '12 at 23:00
  • @DariaEgelhoff as noted in my previous comment, this is a very fragile solution that will not behave as intended if the user does not put any extension on their filename (i.e. if they don't put a dot anywhere). I strongly urge you to use Diego Navarro's solution instead as this will give correct results even with no extension specified. Please also mark Diego's answer as accepted as it is the only technically correct answer (hence the 7 upvotes, compared with none for the incorrect answers). – David Mason Mar 28 '12 at 08:24