Demo entry 6644617

change encoding

   

Submitted by Tom Xu on Oct 05, 2017 at 15:20
Language: Python 3. Code size: 1.3 kB.

import os
import shutil


def listfolders(loc):
    files_temp = []
    folders_temp = []
    for way in os.listdir(loc):
        if os.path.isfile(os.path.join(loc, way)):
            files_temp.append(way)
        else:
            folders_temp.append(way)
    return files_temp, folders_temp


def movefiles(loc, moveloc):
    files, folders = listfolders(loc)
    for file in files:
        if not file.find('._'):
            pass
        elif not(os.path.splitext(file)[-1] != '.txt' or os.path.splitext(file)[-1] != ''):
            shutil.copyfile(os.path.join(loc, file), os.path.join(moveloc, file))
        else:
            with open(os.path.join(loc, file), 'r', encoding='GB18030', errors='ignore') as oldfile:
                temp = oldfile.read()
            with open(os.path.join(moveloc, file), 'w', encoding='utf-8') as newfile:
                newfile.write(temp)
    for folder in folders:
        if not os.path.exists(os.path.join(moveloc, folder)):
            os.mkdir(os.path.join(moveloc, folder))
        movefiles(os.path.join(loc, folder), os.path.join(moveloc, folder))
    return


if __name__ == "__main__":
    movefiles('D:\\色库全书V2007', 'C:\\Users\XUTOM\Documents\\articles2')
    print('Success')

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).