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

lib_0001_file_manager.py的更多相关文章

  1. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  2. Python导入其他文件中的.py文件 即模块

    import sys sys.path.append("路径") import .py文件

  3. import renumber.py in pymol

    cp renumber.py /usr/local/lib/python2.7/dist-packages/pymol import renumber or run /path/to/renumber ...

  4. python gettitle.py

    #!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...

  5. 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!

    情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...

  6. caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例

    caffe机器学习环境搭建及python接口编译参见我的上一篇博客:机器学习caffe环境搭建--redhat7.1和caffe的python接口编译 1.运行caffe图片分类器python接口 还 ...

  7. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  8. MySqlNDB使用自带的ndb_setup.py安装集群

    在用Mysql做集群时,使用Mysql的NDB版本更易于集群的扩展,稳定和数据的实时性. 我们可以使用Mysql自带的工具进行集群安装与管理:ndb_setup.py.位于Mysql的安装目录bin下 ...

  9. 将做好的py文件打包成模块,供别人安装调用

    现在要将写完的3个py文件,打包. 步骤: 1.新建一个文件夹setup(名字随便取),在setup文件夹下,再新建一个文件夹financeapi. 2.将上面4个py文件拷贝至financeapi文 ...

  10. 使用pyInstaller发布PathMerge的exe版本(py转换成exe)

    前言 PathMerge是用python写的一个辅助文件夹合并的小工具,它的特点是不用担心合并后文件会丢失,旧文件会创建副本保存下来,除非你手动删除. 详情见:python开发目录合并小工具 Path ...

随机推荐

  1. Elasticsearch Mapping类型修改

    背景 通常数据库进行分库分表后,目前比较常规的作法,是通过将数据异构到Elasticsearch来提供分页列表查询服务:在创建Elasticsearch索引时,基本都是会参考目前的业务需求.关系数据库 ...

  2. ubuntu18.08安装eigen

    http://eigen.tuxfamily.org/index.php?title=Special%3AAllPages&from=&to=&namespace=100 除非 ...

  3. 大规模 IoT 边缘容器集群管理的几种架构-4-Kubeedge

    前文回顾 大规模 IoT 边缘容器集群管理的几种架构-0-边缘容器及架构简介 大规模 IoT 边缘容器集群管理的几种架构-1-Rancher+K3s 大规模 IoT 边缘容器集群管理的几种架构-2-H ...

  4. 题解 [ZJOI2007]棋盘制作

    把悬线法这个坑填了,还是很简单的 qwq. 悬线法一般解决有一定约束条件的最大矩形问题.悬线的定义是从一个点一直往上走直到边界或者不符合条件结束. 炒个例子,在这题里面比如说有这样一个矩形 0 1 0 ...

  5. 跟女朋友介绍十个常用的 Python 内置函数,她夸了我一整天

    内置函数是什么 了解内置函数之前,先来了解一下什么是函数 将使用频繁的代码段进行封装,并给它起一个名字,当我们使用的时候只需要知道名字就行 函数就是一段封装好的.可以重复使用的代码,函数使得我们的程序 ...

  6. Vs出现调试问题

    当前不会命中断点还未为文档加载任何符号--问题探究 项目运行后断点位置出现以下问题: 原因: 对项目点击进行了发布,导致这里自动切换成了release 切换成debug就正常了

  7. 微信小程序分享百度网盘文件的实现思路

    需求: 在小程序中点击按钮,获取百度网盘文件的下载地址. 实现思路: 1.网盘文件的下载地址,使用官方API只能自己下载,别人通过dlink无法下载,所以采用网页端生成接口. 好处是可以自定义提取码, ...

  8. 马哥教育第一周作业N67044-张铭扬

    1. 图文并茂解释开源许可证 GPL.BSD.MIT.Mozilla.Apache和LGPL的区别? 1)MIT许可证:MIT是六种开源许可证中最自由宽容,它允许使用者自由修改后无需放置版权说明并且可 ...

  9. transformers 之Trainer对应的数据加载

    基础信息说明 本文以Seq2SeqTrainer作为实例,来讨论其模型训练时的数据加载方式 预训练模型:opus-mt-en-zh 数据集:本地数据集 任务:en-zh 机器翻译 数据加载 Train ...

  10. 查看git的用户名和密码

    转载自:https://www.cnblogs.com/xihailong/p/13354628.html 一.查看查看用户名 :git config user.name查看密码: git confi ...