Copy files and catalogs with bar progress [don't work]

Hi.
I try write coping with folder.
Coping works, but progress bar no and I don’t know what doing …
Thanks.

import shutil
import os
import itertools

from time import sleep
from tqdm import tqdm

# path to source directory
src_dir = '/home/piotr/.cache/'

# path to destination directory
dest_dir = '/home/piotr/.cache/0/'

# getting all the files in the source directory
files = os.listdir('/home/piotr/.cache/')

# progress bar start
for files in itertools.count():
        tqdm(range('files'))
# progress bar finish
    
shutil.copytree('/home/piotr/.cache/', '/home/piotr/.cache/0/')

There’s a number of things wrong with your script.

This loop: for files in itertools.count(): overwrites this assignment: files = os.listdir('/home/piotr/.cache/') Also, you’ve not passed an object to .count()

You need to pass a range value to tqdm, at the beginning of a looped operation:

# a short demo
from tqdm import tqdm
from time import sleep

for x in tqdm(range(100)): # replace 100 with the number of operations
    sleep(0.2)  # to simulate an operation
    # the actual operation/s replace the above sleep()

… so for your use case, that looks to be the number of files to be copied, which you should be able to get from len(files). I’ve not tested that, but on the face of it, that’s what I would try.

[edit done to correct minor typo.]

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type “help”, “copyright”, “credits” or “license()” for more information.