Trying to create function called parseExtension that takes a file name as a string parameter and returns the file extension.
Going to ask users to input a filename ie. Python.exe then print the file extension.
I tried to split the text and a number of issues for me so far. I'm using 2.7.
i.e. Please enter your filename: Python.exe
Your file extension is an: exe

def parseExtension(filename):

Recommended Answers

All 5 Replies

Ya i seen that ty, I wasnt using the gui and needed it with a function but I did see that before and found out how I did it is like this:

if filename.strip('any')[-3:]: #Check if the filename no matter what it is report back 3 spots from the period.
        print filename[-3:]        #Print the filename 3 spaces from 0 to the right.
   filename.split('.')            #Split the filename from the 'period'.  

Best to use module os ...

''' file_splitext.py
get a given file's extension
'''

import os

filename = "myfile.exe"
name, extension = os.path.splitext(filename)
print("name = {}  extension = {}".format(name, extension))

''' result ...
name = myfile  extension = .exe
'''

but the os command is undefined isnt it? i dont understand the import os because I need it to take any user input. All the examples set filename = something then prints it. I need it to take user input no matter what it is and seperate the extension. Even if it kgjbdkjbdkjbdf.exe or python.exe.

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.