0
\$\begingroup\$

I have made this code to copy a folder to another disk, and it works, but I think there is a better way.

If don't use the replace(), it says the folder exists and it is correct. The 'T': is real. Can someone point me to the right path?

The folder_path is the path for the torrent folder. it is a normal path, like: 'D:/FILES/MYFOLDER'. The file_path is a variable as to make a torrent, if a folder=folder_path, if a file=file_path, but its not important for the question.

folder_path = 'D:/FILES/MYFOLDER'
file_path = ''
src = folder_path
folder_path_copy = folder_path.replace('D:/FILES/', '')
destination = 'T:' + folder_path_copy
logging.info('Copying files')

if file_path == '':
    shutil.copytree(src, destination)
    logging.info('Folder copied')
else:
    pass
\$\endgroup\$
4
  • \$\begingroup\$ I have clean the inicial code to improve it. \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 14:05
  • \$\begingroup\$ Please do not edit the question, especially the code, after an answer has been posted. Changing the question may cause answer invalidation. Everyone needs to be able to see what the reviewer was referring to. What to do after the question has been answered. \$\endgroup\$
    – pacmaninbw
    Commented Jan 25, 2022 at 14:07
  • \$\begingroup\$ sorry, thank you, can i improve the code in awnser below it? \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 14:08
  • \$\begingroup\$ folder_path is "D:/FILES/SOMEFOLDER", file_path as nothing to do with the question, because if folder_path is present file_path is not. \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 14:14

2 Answers 2

1
\$\begingroup\$

We can simply copy any folder to any disk by using shutil.copytree() function. I think the name you are using to copy your folder to disk T already exist. So check that. I copied one of my folder from disk C to disk D with following lines of code without using replace() function. Here's the code:

import shutil

# your source and destination path could be different

src = 'C:\\Users\\Lenovo\\PycharmProjects\\practice\\invitations'
destination = 'D:\\invitations'
shutil.copytree(src, destination)

The above code successfully copies the invitations folder in disk D.

\$\endgroup\$
4
  • \$\begingroup\$ This is what i have. \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 13:54
  • \$\begingroup\$ And it gives an error "folder is present". \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 14:03
  • \$\begingroup\$ Which IDE are you using? \$\endgroup\$ Commented Jan 25, 2022 at 14:52
  • \$\begingroup\$ Visual studio code. \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 20:02
1
\$\begingroup\$
  1. Your code is broken, since it has uninitialized variables.
  2. Your empty else block is redundant and can be removed.
  3. You can use pathlib to calculate the relative paths.
from logging import info
from pathlib import Path
from shutil import copytree

src_dir = Path(r'D:\FILES\my_folder')
relpath = src_dir.relative_to(r'D:\FILES')
dest_dir = Path('T:') / relpath
info('Copying files')
copytree(src_dir, dest_dir)
logging.info('Folder copied')
\$\endgroup\$
1
  • \$\begingroup\$ This work, but i feel its not very different about my solution. I will study it. \$\endgroup\$
    – Rose
    Commented Jan 25, 2022 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.