I was looking to copy a whole directory and its files but also printing each file name that its being copied.
I was using a simply call to cp -rf dir dest with os.system but I cant print each filename separately as obvious.
I then thought about listing eash directory file by calling recursively ls with os.system, saving the whole string, split them on an array, and implement a for loop to run os.system(“cp ” file1 + ” des/”) and printing the filename, but it looks like lot of work.
Any better ideas to accomplish this?
You can use
os.walkto get the entire directory listing and use that listing to copy all files iteratively. Something likeAlternatively according to
MatthewFranglen‘s comment you can just doshutil.copytree(src, dst). That will also allow you to ignore things but you’ll need to define a function to do that instead of using an if in the list comprehension.compared to