Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created August 17, 2019 07:57
Show Gist options
  • Save mentix02/a766d70e89b3d8f0ba0be54589810f97 to your computer and use it in GitHub Desktop.
Save mentix02/a766d70e89b3d8f0ba0be54589810f97 to your computer and use it in GitHub Desktop.
gets file name from a path
import pathlib
def get_file_name(path):
return path.split('/')[-1]
def get_file_name_efficient(path):
return path[path.rfind('/')+1:]
path = '/home/name/file.txt'
f = pathlib.Path(path)
print(f.name)
assert f.name == get_file_name(path)
assert f.name == get_file_name_efficient(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment