美文网首页
python 对文件基本操作方法

python 对文件基本操作方法

作者: 9016 | 来源:发表于2021-05-19 21:50 被阅读0次
    import os
    import psutil
    import configparser
    
    
    class Setting_Con(configparser.ConfigParser):
        def __init__(self, defaults=None):
            configparser.ConfigParser.__init__(self, defaults=defaults)
    
        def optionxform(self, optionstr):
            return optionstr
    
    
    
    def getLocalPath():
        # 获取当前脚本位置
        try:
            return (os.getcwd())
        except Exception as err:
            print('获取路径失败: %s' % err)
    
    
    def getParentPath():
        # 获取父级目录
        try:
            return (os.path.dirname(os.getcwd()))
        except Exception as err:
            print('获取路径失败: %s' % err)
    
    
    def getUp2StagePath():
        # 获取父级目录
        try:
            return (os.path.abspath(os.path.join(os.getcwd(), "../..")))
        except Exception as err:
            print('获取路径失败: %s' % err)
    
    
    def append2File(logContent):
        # logContent/写入文件内容: str '104.146334 30.635936'
        try:
            filename = 'log.csv'
            with open(filename, 'a') as file_object:
                file_object.write(logContent)
                # file_object.write(',')
        except Exception as err:
            print('写入文件失败: %s' % err)
    
    
    def getAdbPath():
        # 获取所有盘符下adb.exe路径,并返回第一个路径
        try:
            dList = getDiskList()
            # dListLen = len(dList)
            # print(dListLen)
            result = []
            for index in range(len(dList)):
                # print(dList[index])
                path = dList[index]
                filename = 'adb.exe'
                i = 0
                for root, lists, files in os.walk(path):
                    for file in files:
                        if filename in file:
                            i = i + 1
                            write = os.path.join(root, file)
                            # print('%d %s' % (i, write))
                            result.append(write)
                            # print(result)
            return result[0]
        except Exception as err:
            print('定位adb.exe失败: %s' % err)
    
    
    def getDiskList():
        # 获取当前PC上盘符列表
        try:
            diskList = sorted([driver.device for driver in psutil.disk_partitions(True)])
            return diskList
        except Exception as err:
            print('获取盘符失败: %s' % err)
    
    

    相关文章

      网友评论

          本文标题:python 对文件基本操作方法

          本文链接:https://www.haomeiwen.com/subject/hqbujltx.html