look at file type and determine if the type is correct, print statement, python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackie123
    New Member
    • Nov 2015
    • 6

    #1

    look at file type and determine if the type is correct, print statement, python

    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()
    I have tried something like this but it is giving me a syntax error for the line "if f[f.rfind:] == '.wav':" i am not sure what the error is here, could you help me with what the issue may be?

    Code:
    def fileType(): 
    f = pickAFile 
    print f
    print "File Type:", 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."
    Last edited by jackie123; Nov 11 '15, 05:02 AM. Reason: changing code and updating explanation
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Assuming that you identify a file by the name ending, I always use endswith() or if fname[-4:]==. Note that the variable, pickAFile, may not be known to the function unless it is a global variable.

    Comment

    • jackie123
      New Member
      • Nov 2015
      • 6

      #3
      yes i know that endswith() would work best but it is not allowed in this program. How would I go about fixing what I have above?

      Comment

      Working...