1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #!/usr/bin/python3
# -*- coding:Utf8 -*-
import os
import sys
import time
# import argparse
maxfileload = 1000000
blocksize = 1024 * 500
def copyfile(pathFrom, pathTo, maxfileload=maxfileload):
if os.path.getsize(pathFrom) <= maxfileload:
bytesFrom = open(pathFrom, 'rb').read()
open(pathTo, 'wb').write(bytesFrom)
else:
fileFrom = open(pathFrom, 'rb')
fileTo = open(pathTo, 'wb')
while True:
bytesFrom = fileFrom.read(blocksize)
if not bytesFrom:
break
fileTo.write(bytesFrom)
def copytree(dirFrom, dirTo, verbose=0):
destPath = copydir(dirFrom, dirTo)
dcount = fcount = 0
for filename in os.listdir(dirFrom):
pathFrom = os.path.join(dirFrom, filename)
pathTo = os.path.join(destPath, filename)
if not os.path.isdir(pathFrom):
try:
if verbose > 0:
print('[ + ] Copie en cours de ', pathFrom, ' vers ',
pathTo)
copyfile(pathFrom, pathTo)
fcount += 1
except:
print('[ ! ] Erreur copie de ', pathFrom, ' vers ',
pathTo, ' --annulé')
print(sys.exc_info()[0], sys.exc_info()[1])
else:
if verbose > 0:
print('[ + ] Copie de ', pathFrom, ' vers ', pathTo)
try:
os.mkdir(pathTo)
below = copytree(pathFrom, pathTo)
fcount += below[0]
dcount += below[1]
dcount += 1
except Exception:
print('[ ! ] Erreur création ',
pathTo, ' --annulé.')
print(sys.exc_info()[0], sys.exc_info()[1])
return (fcount, dcount)
def copydir(dirFrom, dirTo):
destPath = os.path.join(dirTo, os.path.basename(os.path.normpath(
dirFrom)))
if not os.path.exists(destPath):
os.mkdir(destPath)
return destPath
def getargs():
try:
dirFrom, dirTo = sys.argv[1:]
except Exception:
print('[ x ] Erreur Utilisation: ', sys.argv[0], 'Source Destination')
else:
if not os.path.isdir(dirFrom):
print('[ ! ] ', dirFrom, 'n\'est pas un repertoire.')
elif not os.path.exists(dirFrom):
os.mkdir(dirFrom)
print('[ - ] ', dirFrom, ' crée!')
return (dirFrom, dirTo)
else:
if hasattr(os.path, 'samefile'):
same = os.path.samefile(dirFrom, dirTo)
else:
same = os.path.abspath(dirFrom) == os.path.abspath(dirTo)
if same:
print('[ ! ] Erreur: ', dirFrom, 'est identique à ', dirTo)
else:
return (dirFrom, dirTo)
'''
def menu():
parser = argparse.ArgumentParser(prog='pycp',
usage='%(prog)s -f Source -t Dest',
description='Utilitaire de Copie Python')
parser.add_argument('-f', required=True, type=str, dest='dirFrom')
parser.add_argument('-t', required=True, type=str, dest='dirTo')
args = parser.parse_args()
if args.dirTo == args.dirFrom:
print('[ ! ] Erreur: ', args.dirFrom, 'est identique à ', dargs.dirTo)
else:
print('[ + ] Copie en cours ... ')
start = time.clock()
fcount, dcount = copytree(args.dirFrom, args.dirTo, verbose=1)
print('[ + ] Copie de: ', fcount, ' fichiers et ', dcount,
'répertoires')
print('[ = ] Durée: ', (time.clock() - start), ' secondes')'''
if __name__ == '__main__':
# menu()
dirstuple = getargs()
if dirstuple:
print('[ + ] Copie en cours ... ')
start = time.clock()
fcount, dcount = copytree(*dirstuple)
print('[ + ] Copie de: ', fcount, ' fichiers et ', dcount,
'répertoires')
print('[ = ] Durée: ', (time.clock() - start), ' secondes') |
Partager