Write a function called parseExtension that takes a file name as a string parameter and returns the file extension. Example: If your function was passed “python.exe” as a string parameter it would return “exe”.
I want it to take userinput and seperate the files from the extension starting at the period. I have tried so many different things I'm lost.

def parseExtension(filename): 

    if filename.strip('any')[-3:]: 
    filename.split('.')            
        print filename[-3:]       


filename = raw_input('Please Enter a File Name: ')

Recommended Answers

All 16 Replies

Hi. Here is a small experiment in the python console

>>> "python.exe".rsplit('.', 1)
['python', 'exe']

It should be easy now !

How would I use it inside a function? I'm not using the terminal been using Cloud 9 so having issues. Thank you for a response. Let me try.

For instance I wanted to split anyones user Input. So if you entered holycrapbbq.exe. I grab the exe.
Or holycrapbbq.pdf
Using user input could be anything.

Using user input could be anything

That's why you have a variable or function parameter named filename. You must use this variable instead of "python.exe".

I got it thank you !! You are correct.

#Write a function called parseExtension that takes a file name as a 
#string parameter and returns the file extension.
def parseExtension(filename): #define the function
    if filename == filename.split('.')[-1]:
        print 'Please Enter a FileName: '
    elif filename != filename.split('.')[-1]:
        print filename.split('.')[-1]


filename = raw_input('Please Enter a File Name: ') #Variable filename takes users input and asks to enter a file name.
parseExtension(filename) #call our function

wudie47 read your post,just 1 day old,
vegaseat did answer with the preferred way os.path.splitext().

>>> import os
>>> filename = 'python.exe'
>>> os.path.splitext(filename)
('python', '.exe')
>>> os.path.splitext(filename)[-1]
'.exe'
>>> name, extension = os.path.splitext(filename)
>>> name
'python'
>>> extension
'.exe'

snippsat I understand the inputting it into a gui or >>> but I need it to take any user input and not just set filename. Unless I set filename to equal a list of file extensions.

I dont understand how the import os feature works. I been researching just about everything.

snippsat I understand the inputting it into a gui or >>> but I need it to take any user input and not just set filename. Unless I set filename to equal a list of file extensions.

from os.path import splitext

def parse_extension(filename): 
    if filename == splitext(filename)[-1]:
        print 'Please Enter a FileName: ' #Think of what you are doing here
    elif filename != splitext(filename)[-1]:
        print splitext(filename)[-1]

filename = raw_input('Please Enter a File Name: ')
parse_extension(filename)

I dont understand how the import os feature works

os is part of Python standard library,just import os and use it.
Doc Miscellaneous operating system interfaces
Tutorial OS Module

Thanks a TON I get to see it executed helps me understand the flow the of script.

Trying to build on that problem and do a function called fileExtensionExists that takes a list[] of filenames as parameter one and a file extension string as parameter two.
The fileExtensionExists function must implement a for loop that evaluates the extension of each file in the list using a second function named parseExtension.
-The parseExtension function receives the file name string as a parameter and returns the file extension string of the file name string.
-This is all I came up with, having a hard time getting both functions to communicate and print anything I input.
-Using my 1st function I would like to return a file and the extension seperately from the file list. Please help been struggling with this for a week. Thank you.

    if fileName.strip('.')[-1] != fileList:
        print 'Please enter a valid File Name: '

fileList = (['python.exe','java.exe','myassign.py','mydoc.doc'],'doc')
fileExtension = fileList

def fileExtensionExists(fileList, fileExtension):


    for fileName in fileList:
        if fileExtension == fileName.strip('.')[-1]:
            fileExtension == fileName('.')[-1]
            print 'nope'
            break

        if fileName.strip('.')[-1] != fileList:
            print 'yep'
            break

for fileName in fileList:
        print fileList, fileExtension


fileName = raw_input('Please enter a file name:')        
fileExtensionExists(fileList, fileExtension)

It looks easy if you write pseudo-code

def fileExtensionExists(fileList, fileExtension):
    for fileName in fileList:
        get this filename''s extension, call this 'extension' (use parseExtension)
        if extension == fileExtension:
            return True
    # if we reach here, the extension was not met
    # it means that the extension does not exist in the list
    return False

def parseExtension(fileName):
    if there is a dot in filename:
        return everything after the last dot
    else:
        return the empty string

Now you should be able to transform this into python.

Thnak you for responding, as you can see I have a a list, how can I check against that list while performing those operations? Does yours do that Im trying now.

fileList = (['python.exe','java.exe','myassign.py','mydoc.doc'],'doc')

No, fileList is ['python.exe','java.exe','myassign.py','mydoc.doc'] and fileExtension is 'doc'.

Ok I'm having terrible time trying to check the loop which i thought was easy and I had working.
This is my latest code does it make any sense?

from os.path import splitext

def parse_extension(filename): 

    if filename == splitext(filename)[-1]:

        print 'Please Enter a FileName: ' #Think of what you are doing here

    elif filename != splitext(filename)[-1]:

        print splitext(filename)[-1]

        return

filename = raw_input('Please Enter a File Name: ')
parse_extension(filename)

fileList = (['python.exe' , 'java.exe' , 'myassign.py' , 'mydoc.pdf'], 'doc')
fileExtension = ['.exe' , '.exe' , '.py' , '.doc' , '.pdf']



def fileExtensionExists(fileList, fileExtension):

    while True:

        if filename == fileList:
            print 
fileExtensionExists(fileList, fileExtension)

Thanks again for all the help, my elif isnt working properly and now if it is true it returns it 5 times so the loop kind of works.

filename = raw_input('Please Enter a File Name: ')
parse_extension(filename)

fileList = ('python.exe' , 'java.exe' , 'myassign.py' , 'mydoc.pdf')
fileExtension = ('doc')

def fileExtensionExists(fileList, fileExtension):
    for filename in fileList:


        if filename == fileExtension or fileList:
            print 'Valid filename'
            return

        elif filename != fileExtension or fileList:
            print' Please re-enter a valid filename'
            return

parse_extension(filename)
fileExtensionExists(fileList, filename)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.