Skip to content

Instantly share code, notes, and snippets.

@raghughanapuram
Last active November 14, 2017 09:55
Show Gist options
  • Save raghughanapuram/54b259a1610ba409060c24c271756b98 to your computer and use it in GitHub Desktop.
Save raghughanapuram/54b259a1610ba409060c24c271756b98 to your computer and use it in GitHub Desktop.
Python extract filename and extension from filepath
#Python extract filename and extension from filepath
import os
inputFilepath = "path/to/file/foobar.txt"
filename_w_ext = os.path.basename(inputFilepath)
filename, file_extension = os.path.splitext(filename_w_ext)
#filename = foobar
#file_extension = .txt
path, filename = os.path.split("path/to/file/foobar.txt")
# path = path/to/file
# filename = foobar.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment