Start Automating Your Life Using Python! (File Management with Python Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be going through how you can start using python to automate the boring stuff in your life and we're going to be doing this by going through a project that i built recently that allows me to completely automate my desktop downloads folder management and i'm also going to be sharing you my general principles of what i'm thinking about when i'm building coding projects which has been a highly requested topic on this channel so let's get started what i really love about coding is that it's not just a skill to allow yourself to build a very lucrative and nice career it's also a skill to solve actual problems that you have in your life and just make the stuff that i'm doing anyway more efficient and this is particularly exciting to me because what really got me into coding initially was the appeal of having this superpower to start teaching my computer to essentially do stuff for me and act sort of as this unpaid assistant that i have access to at all times but the first question you might have is well why do we need to use python if i know c or java or some other language can we use those as well and the answer is probably yes you can probably use any language you want all you need to do is google to see if that language has the ability to do the things that we are about to do but the primary reason we use python is because python simply has a lot of libraries and a lot of tools that make it very easy to do these kinds of automation projects that we're about to build it's also very easy to learn and very simple to use so yes you can probably use any language but in this video we are going to be using python and i do recommend using python for these purposes because it's a very sleek language sleek just like a sponsor morning brew do you ever feel like that you want to be able to keep up with the world but there's simply so much going on that becomes overwhelming that's exactly how i used to feel too keeping up with current affairs seemed like a chore rather than something interesting and every morning i would always instead just gravitate to scrolling on social media that's why i'm so glad i found morning breath morning brew is a free daily newsletter that gets you up to speed in business finance and tech in just five minutes and what i particularly love about morning group is they make reading news actually fun and informative rather than a dry and dense style of most mainstream media for example without morning brew i would probably never have got myself to learn about the interesting details of why netflix's stock suddenly collapsed by 35 last week and what this actually means so i can say that being subscribed to morning brew has genuinely made me smarter the best part is that it's completely free and takes less than 15 seconds to subscribe so if you're interested in business finance or tech like i am there is absolutely no reason not to subscribe you can sign up for free by using morningbrewdaily.com internet made coder or by clicking the link in the description thank you for morning proof of sponsoring this video and supporting this channel okay on the project now so what our goal here is to do is that i built this program which don't worry we'll get into how i've done all of this but when i actually run this program and while it's running what i want to happen is that whenever i download an image instead of it going into the downloads folder it's going to be sent to this downloaded images folder and whenever i download a video i want to be sent to the downloaded video folder for example let's say i'm downloading this image of the like button which you should hit by the way so as you can see i downloaded it right there and when you go to the downloads folder it's not there and if we go to my downloaded images folder just go on the image folder so this is the goal that we're gonna be doing here um okay so let's just talk about how i sort of think about approaching this is obviously a lot of things you need to do you need to have the file running you need to have it sort of listening to changes in the downloads folder and then you need to basically based on the file type have the program send it to the appropriate folder so there's a lot of different things that you need to figure out and once you start a project like this you won't really have an idea of doing either of these things so where do you even start well the best way to think about this is to not even worry about all these things at the same time well one way you might think about it is like okay what is the first thing we need in order to have a program that basically moves files around the momentum future the first thing i thought is that first of all we need a way for our python code to access files in our computer so that's the first thing we're gonna figure out and so how are we gonna figure that out well just like any programming program you go to google you go how to access files on mac using python or something like that and basically this is essentially what i did when i was coding this up i think it was this article that i found where i found that okay first of all i need to import this os library so we're gonna do that and then with this library if you use this scan dir method we can essentially return a list of all the files in that folder so we're going to be doing that so if we define the folder that we want what we can then do is a basic for loop on this entries list which is essentially just a list of all the files in the folder so we're gonna go four and three in entries basically this code is gonna be run for each object in that list where entry represents that object in turn so if we just try running that we're gonna stop that python and these are essentially all the names of all the files in my downloads folder currently okay so now we've achieved something we've found a way to access the files in our downloads folder on our computer first step done now we can start worrying about the next thing which is like do something whenever there is a change in my download folder so whenever a file enters my downloads folder well you're gonna google that as you do with anything and essentially what i found is there's probably a better way to do it but one way is through this watchdog library so you're going to install it by going to the installation page you're going to copy this if this doesn't work you might want to try doing pip 3 install it depends on sort of the version of python you have and something like that but essentially you would go through that you would press enter it would install it i've already done it now we have our library installed and essentially what this library does allows you to listen for changes in your folders which is exactly what we want and i'm going to leave all these links down below in the description i do recommend that you go through them in more depth if you want to do this so that you fully understand what you're doing they usually have some sort of starter code for you to get started yeah you just copy it it's going to tell you all the things that you need to import essentially all of this code which now we can go over to my actual file i guess i've got it down here all of this just initiates all these processes you don't even need to know what all of this does all the time the only thing you need to change here is that for this event handler variable you're going to set it to equal an instance of this class that you created yourself of this mover handler card this could be called anything this would be called hello and then in here you would just go and change this to hello like that doesn't matter that you can define yourself and then the other thing you need to do is with this path variable you need to set this to equal the directory that you want to track in this case i want to track changes in my downloads directory and here i've set path equals source directory so these are the only things you need to change in this piece of initializer code so once you've done that essentially what happens is whenever there is a change in this source directory which is my downloads folder it is gonna fire the unmodified function of the mover handler class which is a subclass of the file system event handler class that is the requirement that's what happens when you've got all this setup now we need to start defining the actual code of what is going to happen when there is the change in this folder so now we get to use the scanning the directory code that we figured out earlier as we did before with os dot scanner and in here the source directory that we want to track has entries we're gonna do a for loop for all of the files in that folder then there are all these if statements for example if you're looking at this piece of code here if the name of the file remember we're going through all the files one by one the name of the file ends with dot mov or dot mp4 this is the most common ways video files and the main most common video file types we're going to do is set the destination folder to be test underscore underscore videos which will be defined up here to be this folder and again this is what you would define yourself you would define where you want all your video files to go for example um so we're going to define that as the destination and then we're going to run this move function which is something that i have defined completely myself and that is up here so what is going to happen it's going to take in the destination folder which we just defined and we're going to take the entry and it's going to take the name of the file what happens here is we're first checking for whether that file already exists so we're going to get that in a second but if it doesn't exist so if file exists is essentially not true it's not gonna run this code and it's necessarily gonna go to this shuttle dot move and this is again another thing which i had to figure out by googling now that we know how to access files on our computer how can we through our python code move files in our computer from one directory to another and the way we do this is using the shuttle library which the only way i know that is just by finding it on google i'm just saving you the trouble by showing it here and this shuttle library has this move function so what this is just gonna do is move the file in the destination folder and that essentially completes what we wanna do and then we just do the same thing for images well if it ends in jpeg or png you do the exact same thing except for these images or this downloaded images folder and that's what you saw happening earlier when i was downloading that like button image by the way if you enjoyed this video do hit the like button the only slightly more complicated thing was for music and sound effect but then music and sound effects you have the same file type so i had to figure out how i could separate them but and this is not perfect but essentially i went if the size of the file is less than this is i think this is like 250 megabytes or if the name of the file has sfx so i found a lot of the sound effects i was downloading the name included this sfx string so if the name includes sfx or if the file is small enough it's probably a sound effect rather than music stone in that case i would move it to my destination sound effects folder i could keep doing this for pdfs for docs files or whatever and i probably will but this is just the first version i have now and it serves more of the purposes that i needed for at this time i am going to leave this exact code in the description box where you can essentially use this exact same code if you want or obviously i encourage you to do this yourself as well because it is a very great exercise on python and actually building something for yourself that you actually end up using it for yourself because actually building a program that i use myself is a really rewarding feel like i built from scratch a program that actually is helping to solve problems for me that's a really nice feeling whatever thing you can think of that you're doing on your computer all the time over and over again some like mechanical tasks there's probably an easier way to do it with code and so i just challenge you to try to think of these things that you might be able to automate and then just go on google type on how to do x on python and so that is how we learn and building something that you actually use yourself is a very motivating and a very rewarding way for you to get better at coding whenever you are running what you can obviously do is just cd into the folder and then run the actual program but you'll probably find that that defeats the purpose of a lot of the automation if you then always have to go to the right folder to run the function so what i'd ideally want to happen is that this function is just running all the time so whenever i download something this program is just always running i haven't figured out how to do that but what i have figured out how to do well in my root folder which opens whenever i open a new terminal window i have this automate dot sh file which is actually all this does is you if you look at what's actually in this it bundles these two commands of going into the folder where this program exists and then running the program into this one file that you can just run using one command if you want to learn more about command line scripting and how that can really help you i'm very happy to make a separate video about that so definitely subscribe if you want to do that this is a whole new area of programming i'm personally just getting into and learning all the different productivity things and the things that this can really automate in my life i'm super super excited about but now i encourage you to try this i encourage you to try different automation projects that you might have try to think about problems that you have yourself and if you're a complete beginner in coding you might want to check out my how i learned how to code in four months video is by far the most successful video on this channel i've received so many messages from people that have told me that it has motivated them to start learning yeah definitely go watch that video next right after this one thanks for watching
Info
Channel: Internet Made Coder
Views: 675,975
Rating: undefined out of 5
Keywords: python automation projects, automate the boring stuff with python, learn python, python automation, automate your life, learn python fast, python fundamentals, bash scripting, learn to code, coding projets, coding project ideas, python tutorial, desktop automation, coding tutorial, python 101, how to automate tasks for beginners
Id: NCvI-K0Gp90
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Sat May 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.