Grammalecte  Diff

Differences From Artifact [871d0d4238]:

To Artifact [9aa31c58b5]:


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78






79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
    "make an empty folder or erase its content if not empty"
    if not os.path.exists(sp):
        os.makedirs(sp, exist_ok=True)
    else:
        eraseFolder(sp)


def copyFolderContent (spSrc, spDst):
    "copy folder content from src to dst"
    try:
        shutil.copytree(spSrc, spDst)
    except OSError as e:
        if e.errno == errno.ENOTDIR:
            shutil.copy(spSrc, spDst)
        else:
            raise


def moveFolderContent (spSrc, spDst, sPrefix="", bLog=False):
    "move folder content from <spSrc> to <spDst>: if files already exist in <spDst>, they are replaced. (not recursive)"
    try:






        for sf in os.listdir(spSrc):
            spfSrc = os.path.join(spSrc, sf)
            if os.path.isfile(spfSrc):
                spfDst = os.path.join(spDst, sPrefix + sf)
                shutil.move(spfSrc, spfDst)
                if bLog:
                    print("file <" + spfSrc + "> moved to <"+spfDst+">")
    except:

        raise


def fileFile (spf, dVars):
    "return file <spf> as a text filed with variables from <dVars>"
    return Template(open(spf, "r", encoding="utf-8").read()).safe_substitute(dVars)









|







|





>
>
>
>
>
>







|
>
|







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    "make an empty folder or erase its content if not empty"
    if not os.path.exists(sp):
        os.makedirs(sp, exist_ok=True)
    else:
        eraseFolder(sp)


def copyFolder (spSrc, spDst):
    "copy folder content from src to dst"
    try:
        shutil.copytree(spSrc, spDst)
    except OSError as e:
        if e.errno == errno.ENOTDIR:
            shutil.copy(spSrc, spDst)
        else:
            print("Error while copying folder <"+spSrc+"> to <"+spDst+">.")


def moveFolderContent (spSrc, spDst, sPrefix="", bLog=False):
    "move folder content from <spSrc> to <spDst>: if files already exist in <spDst>, they are replaced. (not recursive)"
    try:
        if not os.path.isdir(spSrc):
            print("Folder <"+spSrc+"> not found. Can’t move files.")
            return
        if not os.path.isdir(spDst):
            print("Folder <"+spDst+"> not found. Can’t move files.")
            return
        for sf in os.listdir(spSrc):
            spfSrc = os.path.join(spSrc, sf)
            if os.path.isfile(spfSrc):
                spfDst = os.path.join(spDst, sPrefix + sf)
                shutil.move(spfSrc, spfDst)
                if bLog:
                    print("file <" + spfSrc + "> moved to <"+spfDst+">")
    except Error as e:
        print("Error while moving folder <"+spSrc+"> to <"+spDst+">.")
        print(e)


def fileFile (spf, dVars):
    "return file <spf> as a text filed with variables from <dVars>"
    return Template(open(spf, "r", encoding="utf-8").read()).safe_substitute(dVars)