1

working on a program that takes a file and determines what type it is. It must be a .wav or .jpg file to be correct, and if it is not one of those, it will display a message that says to choose a correct file type. However, I am stuck on how to get it to determine what type of file is being chosen, since for this program I am not allowed to use f.endswith(), so I was thinking range may work but I am unsure what the paramaters should be, or if range is even the correct choice for how to determine the file type. help please :)

def fileType():
  f = pickAFile
  print f
  for f in range ():
      start = f.rfind(".") 
      if start!=-1:

          print A .wav file type was selected

          print A .jpg file type was selected
      else:
          print No file type was chosen
          print Please choose a different file type

ok I have tried something like this but it is giving me a syntax error, could you help me with what the issue may be?

def fileType(): 
f = pickAFile 
print f f[f.rfind('.'):] 
  if f[f.rfind('.'):] == '.wav': 
    print "A sound file was selected." 
  if f[f.rfind('.'):] == '.jpg' : 
    print "An image file was selected 
  else: 
    print " This was an incorrect file type." 
    print " Please chose a .wav or .jpg file."
Lauren
  • 33
  • 4
  • What language? You can either do an `EndsWith("wav")` or a substring of the file – Kevin Mee Nov 09 '15 at 21:24
  • @KevinMee it looks like Python, and the question says *"I am not allowed to use f.endswith()"*. – TessellatingHeckler Nov 09 '15 at 21:31
  • @TessellatingHeckler I am unfamiliar with python, and since an EndsWith() wont work it seems like a substring is a good approach – Kevin Mee Nov 09 '15 at 21:33
  • yes its in python, but what do you mean when you say substring? I am suppossed to use something that tests the length of the file name, like len(f) and then find how long the end is before the '.' at the end – Lauren Nov 09 '15 at 21:37
  • Names don't, in fact, define the type of a file. The actual contents of the file define the type. The [libmagic library and the file command](https://en.wikipedia.org/wiki/File_%28Unix%29) have a database of heuristics for identifying file types. (FYI) – dsh Nov 09 '15 at 21:38
  • @Lauren You can split the string on '.' , count the length and then check the extension. `string[] array = filename.Split('.');` then assuming the filename is something.ext you can get the length of `array[0]` and then compare `array[1]` to see if it is equal to `wav or jpg` – Kevin Mee Nov 09 '15 at 21:43
  • Possible duplicate of [Checking file extension](http://stackoverflow.com/questions/5899497/checking-file-extension) – skrrgwasme Nov 09 '15 at 21:47

2 Answers2

0

string slicing is what you are looking for, for example:

>>> s = 'file.wav'
>>> s[s.rfind('.'):]
'.wav'

s[index:index2] returns a copy of s that starts at index and ends, but does not include, all characters to index2

you can then do a check like:

s[s.rfind('.'):] == '.wav' # this statement is == s.endswith('.wav')

using string slicing

>>> s = 'file.wav'
>>> s[-3:]
'wav'
R Nar
  • 5,465
  • 1
  • 16
  • 32
  • ok I have tried something like this but it is giving me a syntax error, could you help me with what the issue may be? def fileType(): f = pickAFile print f f[f.rfind('.'):] if f[f.rfind('.'):] == '.wav': print "A sound file was selected. if f[f.rfind('.'):] == '.jpg' : print "An image file was selected else: print " This was an incorrect file type." print " Please chose a .wav or .jpg file." – Lauren Nov 10 '15 at 01:22
  • i cant really help with syntax issues if its in the comments, maybe add an edit in your original post with the code – R Nar Nov 10 '15 at 16:18
  • yes. but what is `PickAFile`? because if it is a method, it should have brackets – R Nar Nov 10 '15 at 16:59
  • and your `print f f[f.rfind...` line should be `print f[f.rfind...]` (you have an extra `f`) I am also assuming your indent errors are just when you posted to SO – R Nar Nov 10 '15 at 17:00
  • yes there should be brackets after pickAFile that was my error, i did not know there was indent errors what needs to be fixed? the main error its giving me is that the line "if f[f.rfind:]=='.wav':" is not legal jython so am i unsure how to fix that issue... – Lauren Nov 10 '15 at 18:17
  • should be `f.rfind(',')` – R Nar Nov 10 '15 at 18:20
  • no it is ('.') but what is not working is that it is not recongnizing the ending to be .wav or .jpg, is there a way i could just tell it that if the length of the ending of the file is 3 letters to print a statement? – Lauren Nov 11 '15 at 19:52
  • sorry yes, that was supposed to be a `.`. well you can jsut string slice like `s[-3:] to get the last 3 characters, see above – R Nar Nov 12 '15 at 02:22
-2

Perhaps you can use the contains method. You would write it something like this FileVariable.contains("png"), within an if statement. These conditions would not be statements. Let me just write it out to be clear.

if (!(FileVariable.contains("fileextension"))
     && !(FileVariable.contains("otherfileextension") {

    // if it  makes  it past this sort of  filter
    // then  it will only be using the  file types that you mention

}

You could also instead of saying each one that can't be used make a filter that tells it what can be let through. This would be much simpler.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28