From time to time, a very common task might be to get the file name, extension, parent folder, or checking the exitance of a file or file list. In Python, this can be done by the in-built os.path library. Here is how!
import os # check interested files is_file = lambda x: os.path.isfile(x) # check if all the files in the input list exist is_files = lambda x: all([is_file(y) for y in x]) if isinstance(x,list) else is_file(x) # get file name get_filename = lambda x: os.path.splitext(os.path.basename(x))[0] # get file extension get_file_ext = lambda x: os.path.splitext(os.path.basename(x))[-1] # get the path of the file get_dir_name = lambda x: os.path.dirname(x) # get he parent folder name of the file get_parent_dir_name = lambda x: os.path.basename(os.path.dirname(x))
No comments :
Post a Comment