current position:Home>Get, rename, and delete file names in Python projects

Get, rename, and delete file names in Python projects

2022-05-15 05:35:28A small pit of goods

File name suffix and name interception

File rename

def file_rename(oldfilename,newfilename):
    """ @Description:  File rename  """
    os.rename(oldfilename,newfilename)

Get file suffix

def get_file_extension(filename:str):
    """ @Description:  Get file extension  :param filename  file name  :return  First return value band .  The second return value does not contain . """
    file_extension = os.path.splitext(filename)[-1]
    if file_extension.startswith('.'):
        #  If the suffix is with . Start 
        file_extension_name = file_extension[1:]
        #  for example  .csv csv
        return  file_extension,file_extension_name
    return file_extension

Get the file name

def get_filename(filename:str):
    """ @Description:  Get the file name from the path  :param filename  File pathname  """
    #  Remove the path 
    full_filename = os.path.basename(filename)
    #  Get the file name 
    return os.path.splitext(full_filename)[0]

Get file path

def get_filename_dir(filename:str):
    """ @Description:  obtain filename Middle way  :param filename  File pathname  """
    return os.path.dirname(filename)

File deletion

Delete file

def delete_file(filepath:str):
    """ @Description:  Delete file  """
    if os.path.exists(filepath):
        os.remove(filepath)
        print(' File deleted successfully , The deleted file path is : ',filepath)
    else:
        print(' file does not exist , Please check the path :',filepath)

Delete empty folder

def delete_empty_dir(dir_path:str):
    """ @Description:  Delete empty folder  :param dir_path  Folder  """
    if os.path.exists(dir_path) and os.path.isdir(dir_path):
        os.rmdir(dir_path)
        print(' Folder deleted successfully , The deleted folder path is : ',dir_path)
    else:
        print(' Folder does not exist , Please check the path : ',dir_path)

Delete multiple empty folders

def delete_empty_dirs(dir_path:str):
    """ @Description:  Delete empty folder  :param dir_path  Folder  """
    #  Only subdirectories and sub files will be deleted 
    for root, dirs, files in os.walk(dir_path, topdown=False):
        for name in dirs:
            # print(' The folder to delete is : ',os.path.join(root,name))
            delete_empty_dir(os.path.join(root, name))

Delete files and folders ( Including the root directory )

def delete_dir_include_root(dir_path:str):
    """ @Description:  Delete temporary files and folders , Will delete the root directory dir_path :param dir_path  Need to delete folder path  """
    shutil.rmtree(dir_path) #  Will delete dir_path root directory 

Delete files and folders ( Not including the root )

def delete_dir_not_include_root(dir_path:str):
    """ @Description:  Delete subdirectories and sub files , Does not contain the root directory  :param dir_path  Folder path to be deleted  """
    #  Only subdirectories and sub files will be deleted 
    for root,dirs,files in os.walk(dir_path,topdown=False):
        for name in files:
            delete_file(os.path.join(root,name))
        for name in dirs:
            delete_empty_dir(os.path.join(root,name))

copyright notice
author[A small pit of goods],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/135/202205142240050078.html

Random recommended