By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,091 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

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

P: 6
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?

Expand|Select|Wrap|Line Numbers
  1. def fileType(): 
  2. f = pickAFile 
  3. print f
  4. print "File Type:", f[f.rfind('.'):] 
  5.   if f[f.rfind('.'):] == '.wav': 
  6.     print "A sound file was selected." 
  7.   if f[f.rfind('.'):] == '.jpg' : 
  8.     print "An image file was selected 
  9.   else: 
  10.     print " This was an incorrect file type." 
  11.     print " Please chose a .wav or .jpg file."
Nov 11 '15 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 621
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.
Nov 11 '15 #2

P: 6
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?
Nov 11 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.