Transparent Battery

キリシマがてきとーする場所

memo / iniCreate

# -*- coding: utf-8 -*-
def getPath(needItem):
    import os
    
    corePath = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH")
    returnPath = []
    
    if "Desktop" in needItem:
        returnPath.append(corePath + "\\Desktop")
    if "Documents" in needItem:
        returnPath.append(os.getenv("HOME"))
    if "Pictures" in needItem:
        returnPath.append(corePath + "\\Pictures")
    if "Videos" in needItem:
        returnPath.append(corePath + "\\Videos")
    if "Music" in needItem:
        returnPath.append(corePath + "\\Music")
        
    elif len(returnPath) == 1:
        return returnPath[0]
    elif returnPath:
        return returnPath
    else:
        return None

def iniWrite(path, writeData, keys):
    import os
    if os.path.exists(path) == False: os.mkdir(path)
    if os.path.exists(path):
        with open(path + "\\setting.ini","w+") as f:
            for i in keys:
                f.writelines(i)
                f.writelines(" = ")
                f.writelines(writeData[i])
                f.writelines("\n")
                
def iniRead(path):
    import os
    result = {}
    if os.path.exists(path):
        with open(path + "\\setting.ini","r") as f:
            lines = f.readlines()
            for line in lines:
                key = line[:-1].split(" = ")[0]
                value = line[:-1].split(" = ")[1]
                result[key] = value
    return result
    
keys = ["one","two","three","four","five","six","seven","eight"]
writeData = {x : "test" for x in keys}

if __name__ == "__main__":
    path = getPath("Documents")+ "\\Record"
    iniWrite(path, writeData, keys)
    print iniRead(path)