Skip to content

Instantly share code, notes, and snippets.

@mojaveazure
Created August 6, 2015 20:19
Show Gist options
  • Save mojaveazure/ef5e03d93df16d6729c5 to your computer and use it in GitHub Desktop.
Save mojaveazure/ef5e03d93df16d6729c5 to your computer and use it in GitHub Desktop.
Basename in Python
#!/usr/bin/env python
# A script to get the basename of a file in Python
# Import modules from the standard Python library
import sys
import os
try:
import argparse
except ImportError:
sys.exit("Please install either the argparse module or Python 2.7 or greater")
# Set and parse the arguments
Arguments = argparse.ArgumentParser(add_help=True)
Arguments.add_argument('-f',
'--file',
type=str,
default=None,
metavar="FILE",
help="Full path to file to extract basename from")
args = Arguments.parse_args()
# Get the basename
def basename(f):
base = os.path.basename(f)
return base
# Do the work here
def main():
if not sys.argv[1:]:
sys.exit("Please specify file path")
else:
basename(args.file)
# Run the program
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment