#!/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()

最新文章

  1. (47) odoo详细操作手册
  2. 第三章 Docker 入门
  3. mysql tinyint
  4. GDB多线程调试
  5. mysql 与 oracle 比较(一)group by 容易产生的误解
  6. JSON的解析
  7. Nodejs负载均衡:haproxy,slb以及node-slb - i5ting的个人空间 - 开源中国社区
  8. 老李分享:Android性能优化之内存泄漏2
  9. DAO与DTO
  10. POJ [P2289] Jamie&#39;s Contact Groups
  11. [Swift]LeetCode70. 爬楼梯 | Climbing Stairs
  12. Django积木块11 —— 缓存
  13. 查看端口占用cmd命令
  14. C++如何禁止对象的复制操作
  15. 如何运用kali-xplico网络取证分析?点开看看吧
  16. [No0000C8]英特尔快速存储IRST要不要装
  17. 伯努利分布、二项分布、Beta分布、多项分布和Dirichlet分布与他们之间的关系,以及在LDA中的应用
  18. 【CSS-进阶之元素:focus伪类模拟点击事件】
  19. Android 如何修改默认输入法
  20. 基于ASP.Net Core学习Docker技术第一步:在CentOS7安装Docker平台

热门文章

  1. react 项目的性能优化
  2. 推荐一款新的自动化测试框架:DrissionPage!
  3. 解析关于Tomcat Servlet-request的获取请求参数及几种常用方法
  4. vue-seamless-scroll滚动加点赞衔接处数据不同步问题
  5. C与Java中的动态数组
  6. 四种语言刷算法之47. 全排列 II
  7. elasticsearch 索引数据手动复制注意事项
  8. .Net Core Elasticsearch 时间查询问题
  9. 《用Python写网络爬虫》pdf高清版免费下载
  10. Mysql去重获取最新的一条数据