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
9 1876 Motoma 3,237
Recognized Expert Specialist -
def findExt(filename):
-
return filename.rpartition(".")[2]
-
-
def findExt(filename):
-
return filename.rpartition(".")[2]
-
Is it a filepointer or a Filename path?
bvdet 2,851
Recognized Expert Moderator Specialist - >>> import os
-
>>> p = r'C:\SDS2_7.0\macro\Zip\tem_v1.00.txt'
-
>>> os.path.splitext(p)
-
('C:\\SDS2_7.0\\macro\\Zip\\tem_v1.00', '.txt')
-
>>>
Motoma 3,237
Recognized Expert Specialist
Is it a filepointer or a Filename path?
The filename variable is a string.
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: -
"c:\\test.txt.txt".split(".")[-1]
-
Motoma 3,237
Recognized Expert Specialist - >>> import os
-
>>> p = r'C:\SDS2_7.0\macro\Zip\tem_v1.00.txt'
-
>>> os.path.splitext(p)
-
('C:\\SDS2_7.0\\macro\\Zip\\tem_v1.00', '.txt')
-
>>>
Thanks bvdet. This would be the preferable way of doing it, in my opinion, as it is built into the language.
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 :)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
|
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...
|
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
| |
by: iwdu15 |
last post by:
hi, how can i get the icon associated with a certain file type? thanks
--
-iwdu15
|
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...
|
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...
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |