w3resource

Python: Divide a path on the extension separator

Python Basic: Exercise-106 with Solution

Write a Python program to divide a path on the extension separator.

Sample Solution:-

Python Code:

import os.path
for path in [ 'test.txt', 'filename', '/user/system/test.txt', '/', '' ]:
    print('"%s" :' % path, os.path.splitext(path))
	

Sample Output:

"test.txt" : ('test', '.txt')                                      
"filename" : ('filename', '')                                      
"/user/system/test.txt" : ('/user/system/test', '.txt')            
"/" : ('/', '')                                                    
"" : ('', '') 

Flowchart:

Flowchart: Divide a path on the extension separator.

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get the users environment.
Next: Write a Python program to retrieve file properties.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Unpack variables from iterable:

# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)