1

For example i want to copy C:\Users\RonSolo\Documents\copy\sources\Books\Songs\pictures\Html\css

to

C:\Users\RonSolo\Documents\copy\destination

the result should be

C:\Users\RonSolo\Documents\copy\destination\C\Users\RonSolo\Documents\copy\sources\Books\Songs\pictures\Html\css

I just learned python basics still trying to find a way, if you guys can help me with a solution it would be great. (for practice purposes)

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
ronn solo
  • 13
  • 2

3 Answers3

0

you can use shutil library. you need to replace your "source" and "destination" path to this code:

import shutil
# Source path 
src = ' C:\Users\RonSolo\Documents\copy\sources\Books\Songs\pictures\Html\css'
# Destination path  
dest = 'C:\Users\RonSolo\Documents\copy\destination'
destination = shutil.copytree(src, dest)

"copytree" use for copying directories. for files you can use

shutil.copy
Milad Yousefi
  • 191
  • 2
  • 9
  • C:\Users\RonSolo\projects\Udemy-python-repo\python-code\venv\Scripts\python.exe C:/Users/RonSolo/projects/Copy-with-Structure/Code/copy.py File "C:/Users/RonSolo/projects/Copy-with-Structure/Code/copy.py", line 21 src = ' C:\Users\RonSolo\Documents\copy\sources\Books\Songs\pictures\Html\css' ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated \UXXXXXXXX escape Process finished with exit code 1 – ronn solo Nov 01 '20 at 14:29
0

you can do it as following:

p1 = 'C:\Users\RonSolo\Documents\copy\sources\Books\Songs\pictures\Html\css'
p2 = 'C:\Users\RonSolo\Documents\copy\destination'

ans = p2+'\'+p1
Divyessh
  • 2,540
  • 1
  • 7
  • 24
0

Have a look at pathlib, it makes handling paths easier. And since you're on Windows, paths contain backslashes as separator, which Python interprets as escape sequence - so you have to add the r directive ("raw string literal").

from pathlib import Path

src = Path(r'C:\Users\RonSolo\Documents\copy\sources\Books\Songs\pictures\Html\css')
dst = Path(r'C:\Users\RonSolo\Documents\copy\destination') / str(src).replace(':', '')

# dst
# WindowsPath('C:/Users/RonSolo/Documents/copy/destination/C/Users/RonSolo/Documents/copy/sources/Books/Songs/pictures/Html/css')

Now you can use copytree to copy all the content, see Copy file or directories recursively in Python:

import shutil

shutil.copytree(src, dst)
FObersteiner
  • 22,500
  • 8
  • 42
  • 72