turra turra - 1 month ago 6
Python Question

error copying folder with python

i made a program that copy automatically a usb device.
when it copy the usb it create one folder in correct destination, and one folder in the same path of python program. i want that itcreate only one folder in correct destination! thanks

this is the code:

import shutil
from array import *
import math
import time
import os
import sys
import random
import datetime


def data():
now = datetime.datetime.now()
format = "%d %b %H.%M"
global now_date
now_date = now.strftime(format)
format = "%M"
global minuti
minuti = now.strftime(format)

data()
old_date = now_date

alfabeto = ['A:','B:','F:','G:','H:','I:','L:','M:','N:','O:',] #mancano e,c,d

a = (r'')
b=random.choice('abcdefghilmnopqrstuvz1234567890èòàù')
new_dir = '{}'.format(now_date)
inc = (r'C:\documenti\program\file\collegamenti\'')
incollaa = "".join([inc, new_dir,' ',b])
i=0

while True:
try:
if i==10: i=0
time.sleep(1)
copiaa = "".join([a, alfabeto[i]])
i=i+1
shutil.copytree(copiaa,incollaa)
if not os.path.exists(new_dir):
os.makedirs(new_dir)


break
except FileNotFoundError:
pass

Answer

Your problem is the following lines:

if not os.path.exists(new_dir):  
    os.makedirs(new_dir)

Since new_dir is a relative path (a date string), it will be created in the working folder of your script.

Comments