#!/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. JZOJ 5843.B

    \(Description\) 给定 \(n\) 个正整数序列 ,每个序列长度为 \(m\). 选择至少 \(1\) 个序列,在每个被选择的序列中选择一个元素,求出所有被选择的元素的 \(\gcd\) ...

  2. FreeFileSync:开源的文件同步工具 | Linux 中国

    转载知乎: https://zhuanlan.zhihu.com/p/194778373

  3. HTML5----响应式(自适应)网页设计(自动适应屏幕大小)

    HTML5----响应式(自适应)网页设计(自动适应屏幕大小) 现在,很多项目都需要做响应式或者自适应的来适应我们不同屏幕尺寸的手机,电脑等设备,那么就需要我们在页面上下功夫,但移动端的布局不同于pc ...

  4. UnoCSS 简化 CSS 的书写

    CSS 样式太多,重复写 在学习 UnoCSS 之前,我提出几个问题: 你是否有过写完了 HTML 之后,跳转到 style 写 CSS 这样来回跳转的痛苦? 你是否有过不知道如何给节点取类名的痛苦( ...

  5. Spring Boot 整合 Logback 日志

    Spring Boot 支持 Java Util Logging,Log4J,Log4J2 和 Logback 等日志框架,默认采用 Logback 日志. 在实际 Spring Boot 项目中使用 ...

  6. Vue mixin混入的介绍

    功能:可以把多个组件共用的配置提取成一个混入对象 使用方式: 1.第一步:定义混合,例如: 2.第二步:使用混合(全局混合和局部混合) (1)局部混合 mixins:['XXX'] (2)全局混合 V ...

  7. vscode cmake工程launch和task文件设置

    1.launch.json文件基本设置 { // Use IntelliSense to learn about possible attributes. // Hover to view descr ...

  8. 7. Light (对象)

    Light Mode:模式 Realtime: 实时的.就是当前光照效果是实时的,不包含烘焙效果(即使场景曾经烘焙过) Mixed: 混合的.就是既使用烘焙数据对静态对象(Lightmap stati ...

  9. USACO2023Jan游记

    由于学校要求,过来打 USACO. 似乎要求起码升到白金? 由于是第一次打,只有从铜组开始了. Brouze 简单题,就给核心代码. 30min AK. Leaders http://usaco.or ...

  10. Too many requests in 1 hour. Try again later.的解决办法

    原因 你的梯子用的人太多了,openAI本身就有问答频率限制. 解决办法 换个相对独立,没那么多人用的梯子,找个"安静点"的地区.