0
good night!
on the study level, I am trying to create a script with the following rule:
what I’m trying to do is:
-inside the 'source' folder will have dozens of files.
-i need python to move the files that start with 'A', from 8 in 8 files to a single name subfolder within the destination.
-ie, take 8 files and put in a single sub-folder, then take 8 more and put in another sub-folder, so on,
-when the source folder has a quantity below 8, the process is finished.
I have been 2 days trying to do according to the code below.
I believe you’re missing something simple, but I can’t identify.
I’m grateful if someone can help me.
thank you!
import shutil, os, datetime
# para criar uma pasta de nome único, usei datetime.now
data = datetime.datetime.now()
folder_dist = data.strftime("%d""%H""%M""%S")
# pasta origem
origem = r'D:\origem'
# pasta destino
destino = r'D:\destino' + '\\' + folder_dist # cria uma pasta de nome único
# muda para a pasta origem
os.chdir(origem)
# cria uma lista dos arquivos dentro da pasta origem
files = os.listdir(origem)
# cria uma lista filtrando os arquivos que começam com 'A'
files_a = [f for f in files if f.startswith('A')]
# quantidade de arquivos que quero mover da pasta origem
files_move = 8
# neste ponto preciso que o python mova de 8 em 8 arquivos para dentro de uma pasta única dentro do destino.
# se não houver ao menos 8 arquivos na pasta, o python termina o processo.
while len(files_a) >= 8:
for file in files_a:
os.makedirs(destino, exist_ok=True)
shutil.move(file, destino)
files_move -= 1
if files_move == 0: # sair do loop
break
What mistake are you making? Well, you say that to make life easier for those who try to help.
– Erick Kokubum
hi Erick, good morning, yes I’m sorry :) the error is the following: the script makes the first break, all right, when will make the second break...gives an error speaking q file already exists.
– hanspereira