473,765 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a extension of a given filename

440 Contributor
Hi,

I need to extract the filename extension from the given filename at a particular location

I/P:
say 'C:\Sample\Samp le1.txt'

O/P :
The file etension is '.txt'

Thanks
PSB
May 15 '07 #1
9 1876
Motoma
3,237 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. def findExt(filename):
  2.    return filename.rpartition(".")[2]
  3.  
May 15 '07 #2
psbasha
440 Contributor
Expand|Select|Wrap|Line Numbers
  1. def findExt(filename):
  2.    return filename.rpartition(".")[2]
  3.  
Is it a filepointer or a Filename path?
May 15 '07 #3
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> p = r'C:\SDS2_7.0\macro\Zip\tem_v1.00.txt'
  3. >>> os.path.splitext(p)
  4. ('C:\\SDS2_7.0\\macro\\Zip\\tem_v1.00', '.txt')
  5. >>> 
May 15 '07 #4
Motoma
3,237 Recognized Expert Specialist
Is it a filepointer or a Filename path?
The filename variable is a string.
May 15 '07 #5
ghostdog74
511 Recognized Expert Contributor
Is it a filepointer or a Filename path?
you should also note that rpartition is available in Python 2.5. if earlier, use bv's method, or use the usual split way:
Expand|Select|Wrap|Line Numbers
  1. "c:\\test.txt.txt".split(".")[-1]
  2.  
May 16 '07 #6
Motoma
3,237 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> p = r'C:\SDS2_7.0\macro\Zip\tem_v1.00.txt'
  3. >>> os.path.splitext(p)
  4. ('C:\\SDS2_7.0\\macro\\Zip\\tem_v1.00', '.txt')
  5. >>> 
Thanks bvdet. This would be the preferable way of doing it, in my opinion, as it is built into the language.
May 16 '07 #7
ghostdog74
511 Recognized Expert Contributor
Thanks bvdet. This would be the preferable way of doing it, in my opinion, as it is built into the langage.
hmm..I cannot say if its "built in" (as in really built in , like str(), int) because to use os.path.splitex t, we need to import os. but to use rpartition, as its a string method, just need "path".rpartiti on("") , much like split() method :)
May 16 '07 #8
Motoma
3,237 Recognized Expert Specialist
hmm..I cannot say if its "built in" (as in really built in , like str(), int) because to use os.path.splitex t, we need to import os. but to use rpartition, as its a string method, just need "path".rpartiti on("") , much like split() method :)
I agree with what you are saying; I guess what I was getting at is that the os.path.splitex t was designed specifically for this purpose, whilst rpartition has a variety of uses.
May 16 '07 #9
pyarticles
2 New Member
Check out this Python Articles
May 17 '07 #10

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

Similar topics

3
6265
by: zfeld | last post by:
My app is creating a word document which my app calls saveas() and saves it to a given directory that is being watched by a fileSystemWatcher. The problem is that the fileSystemEventHandler for creation is only called for the temporary filed created by word which shows up as ~$documentName.doc the real file named MyDocument.doc will never invoke the fileSystemEventHandler for creation. Now when the user closes Word the temporary file is...
3
4182
by: cathywigzell | last post by:
I have a web page which allows a user to download a file directly to their computer, using IE. I present a file name in a table and the user can then right click on the file name to get a popup menu with the option 'Save Target As...'. Selecting that option results in the Save As dialog. However, if the filename has a reconginsed extension, e.g. .doc, then the Save As dialog adds another extension, so the file name in the Save As dialog...
8
8562
by: Lad | last post by:
Hello, what is a way to get the the extension of a filename from the path? E.g., on my XP windows the path can be C:\Pictures\MyDocs\test.txt and I would like to get the the extension of the filename, that is here txt I would like that to work on Linux also
3
9879
by: windandwaves | last post by:
Hi Folk I have the following string: http://www.myurl.com/folderbit/moerfiasdfsadf/sdaf/01354.jpg and I want to extract the filename from it. Do you know how to do this, if I don't know the exact names of the folders, etc...
3
9117
by: Phoe6 | last post by:
Hi all, I had a filesystem crash and when I retrieved the data back the files had random names without extension. I decided to write a script to determine the file extension and create a newfile with extension. --- method 1: # File extension utility. import os
1
2433
by: iwdu15 | last post by:
hi, how can i get the icon associated with a certain file type? thanks -- -iwdu15
1
1202
by: J.P. | last post by:
I've developed a com extension for explorer and another dll linked to the com extension which provides the actual functionality. The extension does nothing more than create an object and pass selected files to the object for further processing. The problem I'm running into is with a few cases resources do not seem to be properly released. For example, I can remove all of the files in the given folder where the shell extension was used but...
12
11545
by: Dave H. | last post by:
Please redirect me if this message is posted to the wrong group. Given the intention of delivering content to an HTTP user agent (such as Internet Explorer) which is to be immediately opened by an associated application, there are the Content-Disposition type options of "inline" or "extension-token". Specifically as regards Internet Explorer, I've tried both inline and the specific filename extension (xls,csv,pdf,doc,...) and the...
18
3520
by: Aaron Gray | last post by:
Has anyone got a code snippet to separate out the path components, ie drive, path, filename, and extension ? Many thanks in advance, Aaron
0
9404
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.