Python's method to return the filename and extension for a given file

  • 2020-04-02 14:45:45
  • OfStack

This example shows how python returns the filename and extension from a given file. Share with you for your reference. Specific analysis is as follows:

This code can return the file name and extension based on the full path of the file, and python functions can return two values at once, making it easier to use


def GetFileNameAndExt(filename):
 import os
 (filepath,tempfilename) = os.path.split(filename);
 (shotname,extension) = os.path.splitext(tempfilename);
 return shotname,extension

The test code


print(GetFileNameAndExt('c:jb51index.html'))

Returns the result :('index', '.html')

I hope this article has helped you with your Python programming.


Related articles: