Heyy Buddy!! Ever wondered how you can create a program to move and copy those files for you? Today, I have got just what you are looking for😉.
Copying and moving files/folders is a piece of cake with python’s inbuilt module ‘shutil’. All you need to do is import ‘shutil’ to your python program and follow the steps given below to copy and move the files. So, let’s learn about the different functions this module offers which helps us in moving and copying files.
shutil.copy():
This function copies a file to a new folder. It takes two arguments, the first argument is the original directory and the second one is the destination directory (i.e. where you want to copy the file).
For example, if you have a file named ‘spam.txt’ in your ‘C:\’ drive, you can copy it to a folder named ‘Hello’ in the following way:
shutil.copy(‘C:\\spam.txt’, ‘C:\\Hello’)
You can also copy and rename the file at the same time. Just add the new file name at the end of the destination directory:
shutil.copy(‘C:\\spam.txt’, ‘C:\\Hello\\Hello.txt’)
Now the ‘spam.txt’ file will be copied to the ‘Hello’ folder with the name ‘Hello.txt’.
shutil.copytree():
This function is similar to the previous function, the only difference is that you can use this function to copy an entire folder into a different directory. Even the application is pretty much similar:
shutil.copytree(‘C:\\Hello’, ‘C:\\Hello World\\Hello’)
Note: Whenever you are copying or moving an entire folder, always mention the original name of the folder in the destination directory to avoid renaming the folder.
And just like the ‘copy()’ function, you can even rename the folder while copying.
shutil.copytree(‘C:\\Hello’, ‘C:\\Hello World’)
shutil.move():
And finally, you can move a file to another folder with the ‘move()’ function. The application is exactly the same as the ‘copy()’ function, the only difference is that this will move the file completely instead of copying it to a different folder:
shutil.move(‘C:\\spam.txt’, ‘C:\\Hello’)
You can also rename the moved file just like with the ‘copy()’ function by adding the new file name at the end of the destination directory.
You can also move a folder the exact same way.
Now, let’s say you have a folder named ‘Programming’ in your Desktop, how will you move it to another folder on your Desktop named ‘Computer Science’? Lemme know the solution code to this in the comments 👇👇
CONCLUSION:
AND THAT is how you can use a basic and simple python module to move files around. Now you can use this simple knowledge and turn it into a whole program or simply use it as a part of a more complicated program. I’ll leave the application to your creativity.
Giving this article a like👍 could be a way of telling me that you found this helpful and knowledgeable. Sharing it with your friends shows that you absolutely loved this content and want me to upload more articles like this.
If you have any doubts, queries or questions, comment below or chat with me and other programmers directly on our telegram community, we would love to have you there 😊.
Will see you again in a week, until then keep practicing and….
HAVE AN AWESOME DAY!!!
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback