Created
August 17, 2019 07:57
-
-
Save mentix02/a766d70e89b3d8f0ba0be54589810f97 to your computer and use it in GitHub Desktop.
gets file name from a path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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