473,545 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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
2 1331
dwblas
626 Recognized Expert Contributor
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
jackie123
6 New Member
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

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

Similar topics

8
2241
by: Xenophobe | last post by:
I developed a simple tool to allow users to upload .GIF image files. It works great on my dev box and $_FILES returns "image/gif", but on the prod site it returns "application/octet-stream". Any suggestions would be greatly appreciated.
9
2299
by: François Pinard | last post by:
Hi, people. I hope someone would like to enlighten me. For any application handling Unicode internally, I'm usually careful at properly converting those Unicode strings into 8-bit strings before writing them out. However, this morning, I mistakenly forgot to do so before using one Unicode string (containing a non-ASCII character) as an...
3
2576
by: Shahid Juma | last post by:
Hello All, This may be a trivial question, but I was wondering how can you determine if a value contains an Integer or Float. I know there is a function called IsNumeric, however you can't determine what type it is. Thanks for the help, Shahid
1
2952
by: dcrespo | last post by:
Hi to all, I have functions defined in an xml-rpc server. Some functions query to a Postgres database (using adodb) and return its recordset. So, if some xml-rpc client runs the mentioned function, it will retrieve the recordset. The problem is that if a retrieved field has the Null value or the Date value (DateTime Database format), then,...
4
493
by: CLEAR-RCIC | last post by:
In the old days, you could get a file type in ASP by doing: dim fs, fld, f set fs = server.createobject("Scripting.FileSystemObject") set fld = fs.GetFolder(server.mappath(sCurrentDir)) for each f in fld.subfolders f.type next
2
22660
by: Anthony | last post by:
Hi, I need to print word doc, excel, pdf, images(tiff), html. If this is impossible? Please suggest what i should do? - like handle each type in code separately? thanks
2
2558
by: Ames111 | last post by:
Hi i want to be able to determine what type of file is being dragged on to my form, ive got this: private void Form1_DragDrop(object sender, DragEventArgs e) { //tell me the file type please } private void Form1_DragEnter(object sender, DragEventArgs e) {
10
1877
by: marc | last post by:
Hoi there, I am a Delphi Programer who moved over to C# some months ago. So far I am really happy with C# and feelt myself confortable quite fast. Just some advanced questions regarding the type system are left. One question is the "type of type" issue: In Delphi (Pascal) I was able to do the following (free translation to C#...
3
2537
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external program, but I do not know how I would find the default handler to a file type. Any help is greatly appreciated. Code to launch application:...
1
2426
by: josh wold | last post by:
Here is my problem. I'm using plink to ssh to devices and pull info off. I need to manipulate this data so that extra 0 at the end is making it difficult. C:\Python30>python PLINKSSHtest.py Enter your username: josh Password: plink -pw nowayjose -ssh nope@1.1.1.1 "show run | inc hostname" hostname net-R2 0 <------------MY ISSUE
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.