shutil: The Ultimate Python File Management Toolkit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to cover the core python package called shutil or as some people like to call it shutil which is used for highle file operations in Python so let us get right into [Music] it all right so regarding the pronunciation I'm going to call it shtil I'm sure you're going to let me know in the comment section down below if that's the incorrect way of pronouncing it but I always call it that so I'm going to continue to do it for this video today and the first thing we're going to do is we're going to import the package it's a core python package so we don't need to install any external packages here just import sh util and then we can go through the functionality so in this video today I want to cover the different things that we can do with this package it's a very fundamental package so it's a beginner friendly video and I just want to show you the different functions of course we're not going to cover absolutely everything because there's some uh pretty Advanced things that we can do with this as well we can register for example uh custom archive types and stuff like that we're not going to cover all of this I'm going to go through the basic things that you can do with shu to so that you know how and when to use this package and we're going to start with a very simple thing that we can do which is copy a file so what I'm going to do now is I'm going to create a new file here uh which I'm going to call first.txt for example and here I'm going to have some text hell world I'm going to save this I'm going to close this and then with shuil I can now copy this file to a different direction so I can say shutil do copy I can specify a source path this doesn't have to be just a file name it can also be a path to a specific um to a specific file and then I can also specify another path to the new destination which is going to be second. txt in this case and it's quite simple I run this I get a second file second txt so that's quite simple we can do the same thing now with directories however we cannot just go ahead and say shu to copy and then directory directory uh we have to use a different function namely the copy tree function so copy tree and then I can Define or I can create a directory let's call this uh Source directory and then I can have here the two text files I can copy them into the source directory and now I can say I want to get from Source dear to destination de for example and when I do that you can see I create a new directory three which has these two files in it so that's also quite simple now one thing that's a little bit more advanced even though I don't want to call it Advanced it's a little bit more uh customized is you can ignore specific files so let's say I have a bunch of files here and I want to have a rule for which files to ignore when copying the directory what I can do is I can define a function that uh defines and it has to be a function we cannot just pass a list has to be a function that takes a directory and files and then we can um return a list and this list is basically the files that we want to ignore so I'm going to define a function ignore specific files you can call it whatever you want uh you got to have a parameter directory and you got to have a parameter of files and then what you do basically is you return a collection a list of files that you want to not copy so a very simple thing to do in our case is to just say f for f in files if f is equal to uh first.txt that's a very very basic thing now because that would basically say we ignore this file when copying the uh the the directory so I can delete this again here and I can call the function um shutil do copy tree again from Source directory to destination directory and now I'm going to pass ignore equals and I can pass a function now as a first entity so not by calling it but by just specifying the function and this is going to ignore this specific file so now we're going to see I only have this file copied in here and this is of course useful not for excluding individual files but for excluding for example patterns exclude all the files um so for example what I could do is I could say F ends with and then I can specify a file type so ignore all the txt files or maybe I have a certain pattern that says okay okay um I have some invoices so I can say invoice in F so I want to have all the files copied except for those where invoice occurs in the file name something like that so you can Define your own rules the only thing you need to have is a function the function takes directory and files as parameters and then you return a list with the files that you want to ignore not the files that you want to keep this is important um all right so that's a little bit more customized here what we can also do is we can move and move of course uh is just the same as copy but the first file the initial file the source file um disappears so we just move it somewhere else uh let me just for this delete this again and get at least one file out of here first txt now I can just say sh. move and I can get the first txt file I can move it to another file third. txt for example this is basically just going to rename it but of course I can also go ahead and say third txt I now want to move it to the directory here which is Source deer and then let's call it first txt again um there you go so this is how you move a file using Shu tool um a very simple thing that we can also do just some basic stuff here again is we can remove a tree we can remove a directory just uh Source directory and then when I run this the directory is gone so that's also quite simple and by the way these are things that are um not as easily done with OS sometimes you cannot do them with OS at all all um so sh util is the way to go here um another thing which is more about getting information is you can get the total dis usage of a specific device of a specific uh path and uh what you do here is you get three return values from a function we're going to use here in a second it's going to be total the total space that is available the used space and the free space and this is going to be the result of shutil uh dis usage and then we just specify uh on Linux systems slash to get the current uh root path then of course you need to print that total used free and then you get the bytes uh that are total used and free um yeah that's basically it and you can do that also for different paths here so if you have a USB flash drive you can also uh specify the path here and get this uh this information from there as well now one thing that's very interesting is and this can be useful for for specific use cases uh is you can copy the metadata of files so for example if I create a file here right now um one.txt let's say hello world is um is uh the name now I have certain metadata here in the properties so I have accessed modified created let's just focus on this now um so the last time this file or or maybe the this file was created it um Sunday December 10th and then this time here which is not going to be the time of a second file that I create so let's go ahead create a second file second. txt same content uh what will happen here is when I right click the file you're going to see it has a different time so it's going to have uh 3745 this one has uh 3715 now if I want to take the content of this or the metadata of this file and paste it in here I can do that with a command and actually I should probably change the content of this file so you can see that the content is actually not being changed so I can say hi so this file here has the content hello world this file here has the content high and they have different metadata so what I can do now is I can say sh util copy stat and then I can say one txt second txt and when I run this you're going to see so now you can see when I open the file I still have the content high and when I right click it and go to properties you can see that now I have uh the data here the metad data copied from the first text file so this is a function we can use to copy the metadata without copying necessarily the content and I think that there's actually a function uh copy 2 I think it's uh called where you copy the content and the metadata so you can look it up I'm not sure if that's exactly what it does um another thing that we can do is we can change the ownership of the file so what I can do is I can open up a terminal I can navigate to this direction here um and if I run the LL command you can see that I own all these files um what I can do now here is I can say shutil change owner so CH own and I can change the owner of a specific directory or file let's go with one txt I can change the owner to the following user root for example now for this one I'm going to need root privileges I'm not going to be able to run this uh like this you can see it says um operation not permitted but if I run this with a pseudo at least on Linux then this is going to work I just have to provide my password and then um it's done and when I run LL again you can see that this file is now owned by root so that's nice uh this is a command that is also uh interesting we can also change this back of course to neural 9 and then I'm going to have the same ownership again so that's also kind of useful uh what's also interesting is that we can get the path of a certain keyword so if I use Python 3 I can run my terminal I can say Python 3 this is an executable that I'm running now if I want to know where does Python 3 actually is on Linux what I do is I say which so which Python 3 and it gives me basically the path to this particular python version that I'm using here I can do the same thing here with shutil I can say shutil which and I can say Python 3 I can print the result and now you can see in this case I get a different executable because here I'm using a different python um binary then I'm using in a terminal as it seems and yeah you can do that for different executables as well not just for python um all right and then last but not least I want to show you an interesting feature and this is one that you can go into more detail if you want to maybe I can make a separate video for this as well uh which is unpacking and creating archives and there are some default types supported but you can also go and create your own custom uh unpacking functions that you can then feed into or add as a uh function to shutil whether you want to do that or not is up to you but it's possible so what we can do here is we can say let's create a directory here my files and let's copy these two files into this directory here what I can do now is I can say shutil do um make archive and I can just say the archive name shall be something like um my archive and then I want to specify what kind of archive I want to have I want to have a zip archive for now um and I also want to uh specify which files I want to create an archive from and for this I'm going to just pass my files which is the directory and then when I run this you can see I have this my archive zip if I open it here you can see it contains these two files um and then of course what I can do is I can just go and say shuto unpack archive and I just have to specify my archive zip and path where I want to extract this unpacked deer and then I can run this and you can see I get the files in here so you can do that by default and you can also display what kind of options you have here so you can say sh. getet archive format and also get unpack formats to see what's possible here so you can see by default you have uh bzip 2 gzip you have have tar you have um XZ and you have zip to create archives and then you have these here um which are are they the same I think they're the same right uh for unpacking and if you want to add something else again you can uh I think there's the function uh what's it register archive format which I'm not going to cover now but there is this function you can look it up and if you want to have a video about this let me know in the comment section down below I think this goes a little bit uh too far here but yeah this is how you use the shutil package in Python this is a useful package especially when you combine it with OS and maybe with something like subprocess to run command line um yeah commands terminal commands this is what you usually want to use when you and maybe you want to combine it with the glob package so when you work with files and with archives and with directories and stuff like that with iterating over files and paths and stuff like that those are the packages that you want to use and in this video we covered sh2 so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and [Music] bye
Info
Channel: NeuralNine
Views: 8,888
Rating: undefined out of 5
Keywords: shutil, file management, python, python shutil, copy files, move files, archives, zip, unzip
Id: sXzezIK0d7c
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Sun Dec 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.