Moving files in folders

0

I have a folder called test .

Within this folder there are 1000 sub-folders numbers from 1 to 1000.

Inside each of these subfolders there is another folder called reports

Within each reports folder there is a file called report.json

I need to get each of these report.json files and move them to the destination folder.

I have the following script:

import shutil
import os
import glob

source = "C:\Users\usuario\Desktop\teste\**numero_da_pasta**\reports\"
dest1 = dst = "C:\Users\usuario\Desktop\destino\"

files = os.listdir(source)

for f in files:
    if f == "report.json":
        shutil.move(source+f, dest1)

It is able to move the report.json file to the destination folder, however every time I have to manually change the folder number to 2, 3, 4 etc ...

How do I make this process all automated? I can not do it.

    
asked by anonymous 14.05.2018 / 22:29

1 answer

2

To change the folder name, simply use the following function:

os.rename(old, new)

I understand that you want to number the destination folder, correct?

In this case it would be something like:

import shutil
import os
import glob

source = "C:\Users\usuario\Desktop\teste\**numero_da_pasta**\reports\"
dest1 = dst = "C:\Users\usuario\Desktop\destino\"

files = os.listdir(source)
i = 1
for f in files:
    if f == "report.json":
        shutil.move(source+f, dest1)
    os.rename(dest1 + "destino", dest1 + "destino" + str(i))
    i += 1
    
14.05.2018 / 22:43