lib_0001_file_manager.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import pathlib
import shutil
import sys

class file_manager():
    
    cls_working_folder = 'c:\\'

    @classmethod
    def create_file_in_working_folder(self, file_name):
        pass

    @classmethod
    def create_file_in_current_folder(self, file_name):
        pass
    
    @classmethod
    def create_file_for_nt(self, file):
        parent = os.getcwd()
        if file.find('\\') >= 0:
            parent = os.path.dirname(file)

        self.create_folder(parent)
        try:
            with open(file, 'w') as f:
                pass
        except Exception as e:
            print(e)

    @classmethod
    def create_folder(self, path):
        mk_path = pathlib.Path(path)
        mk_path.mkdir( parents=True, exist_ok=True )

    @classmethod
    def delete_folder(self, path):
        if self.is_a_exist_folder(path):
            try:
                shutil.rmtree(path)
            except Exception as e:
                print(e)

    @classmethod
    def delete_file(self, path):
        if self.is_a_exist_file(path):
            try:
                os.remove(path)
            except Exception as e:
                print(e)

    @classmethod
    def dump_to_file(self, file, context):
        pass

    @classmethod
    def load_from_file(self, file):
        context = ""
        if self.is_a_exist_file(file):
            try:
                with open(file,'r', encoding='utf-8') as f:
                    context = f.read()
            except Exception as e:
                print(e)

        return context

    @classmethod
    def load_lines_from_file(self, file):
        context = []
        if self.is_a_exist_file(file):
            try:
                with open(file,'r', encoding='utf-8') as f:
                    context = f.readlines()
            except Exception as e:
                print(e)

        return context        

    @classmethod
    def append_to_file(self, file, context):
        if self.is_a_exist_file(file):
            try:
                with open(file, 'a', encoding='utf-8') as f:
                    f.write(context)
            except Exception as e:
                print(e)

    @classmethod
    def get_folder_name(self,path):
        pass

    @classmethod
    def get_working_folder(self):
        pass

    @classmethod
    def get_parent_folder_name(self):
        pass

    @classmethod
    def is_a_exist_folder(self, path):
        return os.path.isdir(path)

    @classmethod
    def is_a_exist_file(self, path):
        return os.path.isfile(path)        

    @classmethod
    def get_sub_folder_list( self, path ):
        for root, dirs, files in os.walk(path, topdown=True):
            return dirs

    @classmethod
    def _get_sub_folder_list_2( self, path ):
        dirs = os.listdir(path)
        pass

    @classmethod
    def what_is_this(self, path):
        if not path:
            return "null"

        if self.is_a_exist_file(path):
            return "exist file"
        elif self.is_a_exist_folder(path):
            return "exist folder"  # C: will be ok 

        pos_sep = path.rfind('\\')
        if pos_sep < 0:
            return "invalid path" # dose not like C: 

        if not self.is_a_exist_folder( path[0:path.find('\\')] ):
            return "invalid path"

        ret = ""   
        pos_point = path.rfind('.')
        if pos_point > pos_sep:
            ret = "to make a file"
        else:
            ret = "to make a folder"

        dir_p = path
        while not self.is_a_exist_folder(dir_p):
            dir_p = os.path.dirname(dir_p)
        ret = ret + " with root:" + dir_p

        return ret

def _test():
    # print('hello')
    # print(env_config.root_path)
    # print(env_config.lib_path)
    # print(env_config.data_path)

    #file_manager.get_sub_folder_list(env_config.project_path)
    # print(file_manager.is_a_exist_file("c:\\test\\aaaaa.txt"))
    # print(file_manager.get_parent_folder_name())

    fo_gu = "c:\\test\\guyu\\1234\\2222"
    # fo_gu = 'this is not a path'
    f_gu = "c:\\test\\gu.txt"
    f_gu = "c:\\test\\gu\\gu.txt"
    # f_gu = "c:\\test\\gu\\aa.xlsx"
    # f_gu = "xxx"

    # file_manager.create_folder(fo_gu)
    # file_manager.delete_folder(fo_gu)
    # file_manager.delete_file(f_gu)
    # file_manager.create_file_for_nt(f_gu)
    print(sys.getdefaultencoding())

    file_manager.append_to_file(f_gu, "add a line")

    print(file_manager.is_a_exist_file(fo_gu))
    print(file_manager.is_a_exist_folder(fo_gu))

    # aa = os.path.dirname(fo_gu)
    # print(aa)

    # print(file_manager.what_is_this(fo_gu))
    # path = pathlib.Path(f_gu)
    # path = path.parent
    # path = os.path(f_gu)
    # print(path)

    pass

    
if __name__ == '__main__':
    _test()
posted @   小葱饼子  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示