Learn Python Scripting With This ONE Project!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign you some valuable python scripting skills by running you through a complete project specifically I will be showing you how to deal with command line arguments how you can copy different files or directories search for different files or directories use modules like OS sys sub process and a bunch of other modules that are very helpful when you're trying to create Python scripts that can automate workflows and just make your life easier personally just knowing a few of these commands has helped me and save me countless hours in different development projects and the project that I'm actually working on here with you is kind of a variation of something I actually had to do when I was working for my startup and creating kind of an advanced script that had to go through and automatically compile a bunch of code for me anyways thank you guys will find some great value in this so with that said let's get into the video after a quick word from our sponsor before we get started I need to thank lenode for sponsoring this video I've been using and working with lenode for nearly three years now and and they've consistently been my go-to choice for hosting my applications and servers in fact right now I'm using the node to host the official Tech with Tim Discord bot a website multiple of my domains and a kubernetes cluster now lenode was just recently acquired by Akamai and as a part of this acquisition they're going to be adding more than a dozen new data centers around the world in the next year lenode makes it super easy to spin up servers with their one-click app Marketplace and have tons of written guides and YouTube videos on their channel to help you get started now if you're having any issues you can reach out to the 24 7 support team and actually talk with a real human being now the nodes pricing is simple with no hidden fees and full transparency and you can test out the node services for free by using the link in the description and claiming in a hundred dollar sixty day credit for any new accounts it's been an absolute pleasure to work with lenode over the past few years and one last thank you to them for sponsoring this video alright so let's dive into this project I just quickly want to mention if you don't already know I do have a programming course it's called programming access expert.io great place to become a software engineer and learn how to code specifically in Python and go check it out if you haven't already I'll leave a link in the description alright so let me start by walking you through the project that we're going to complete now this is a project that kind of mocks what you would be doing in a real world scenario obviously I don't know what you guys need to automate or what scripts you need to write so I've tried to create something that's very general that allows you to get a bunch of knowledge in a ton of different areas so this may not cover everything you need to do but it will give you a ton of that base knowledge and show you what's possible with python scripting and how you can kind of set up these scripts to be as effective as possible and what command you should be using for a lot of the common operations so let's have a look at it here first thing that I want to look at is just the directory I have on the left side of my screen now you guys can download This by going to my GitHub I will leave a link in the description of course and just clone the repository download the repository and get access to this data directory here now this data directory is going to kind of simulate kind of a large project some code or some files that you would already have and that you want to Traverse through and do something specifically with now that's usually what automation is or scripting is for me in Python it's going through some existing data or an existing file structure and kind of parsing things out grabbing specific data looking for certain names or writing code into different files so I've tried to kind of emulate that here so you can see we have this data directory here and inside of here I have a bunch of stuff right I have some empty directories I have some directories that are representing kind of games that would be written in go I have some random python files they're just empty just for example purposes I have a text file I have another kind of go file here and you can see that there's a bunch of stuff in this directory now what we're interested in here for this kind of mock project is parsing out all of the game directories so what we want to do is want to take all of the games that are inside of this directory we want to copy these into a new directory we want to change the names of of the directory such that we don't have this game kind of suffix on them and then we want to compile all of the game code that exists inside of these game directories now this is something I actually had to do when I was working for my startup as I mentioned in the intro so I've kind of put a variation on it here now to go even a step further once we get all of this game data we parse it out we put it into a new directory and we compile all of the code I want to write a Json file into that directory that gives us some metadata or information about what we just did so how many games are there what are the name of our game directories Etc and that's something that we can kind of quickly look at to get a summary of the information so you can imagine this would be something similar to what you may want to do when you are writing your own Python scripts okay so I have a kind of the project text here excuse any spelling mistakes that are in this file but I just wanted to walk you through very organized step by step what we're going to be doing so so some assumptions here sorry uh the data directory contains many files and directories so imagine this is massive right there's a ton of different code in here and it's not feasible for us to do this manually we want to have a script that automatically does it for us obviously that's not really the case here but again kind of picture that we had you know thousands of files in that case it would be very helpful to have a script to help us do this you are only interested in the games contained in this directory so you don't care about the rest of the junk in there you just want the games which are going to be stored in a directory that contains the word games so whenever we see the word game and that's in the directory name that we know that we're interested in that directory next each game directory contains a single dot go file that must be compiled before it can be run okay perfect that makes sense and these go files could be named different things notice one is called main one is called code one is called file so we're gonna have to do kind of special stuff to handle these different names and compile all of this code okay so now what are we going to do for this project well we're going to find all of the game directories from the data directory we're going to create a new slash games directory or whatever we want to call it that's going to contain all of our game code we're going to copy and remove the game suffix of all of the games into the games directory so that means copy the directory strip off that kind of game aspect of it put it into the new directory we're going to create a Json file with the information about the games so how many games what the game names are Etc we're going to compile all of the game code and then run all of the game code now we may not actually run it here but we will at minimum go to the compile step perfect so the only thing we need to do here to get started is make sure we have python installed I'm going to be using python 3.9 I believe but I think anything above 3.8 should work and if you want to be able to compile and run this go code then what I'm going to suggest to you is that you download a go interpreter or compiler so let me just bring this up here go code I'll leave this link in the description as well but if you go to go.dev pretty simple URL click download it will download and install go it will automatically add it to your path then you'll have access to the go command that we'll be using later in the video last thing if you want there's some helpful extensions here in vs code if you're using vs code you probably want to install the python extension and the go extension as well okay so now that we've got through that let's start writing our code so I have a python file here called get game data you can call this whatever you want and notice I've put this in the same directory as where my data directory is now you don't need to do that but it is going to be helpful to have it kind of close to the data directory so you know the path to where that is so again if you want go to the GitHub I'll leave that link in the description download this code it will have the finished code as well but you can just kind of delete the file and write it along with me if you want and then you'll have this data directory so you don't have to kind of create this on your own so you want to work in a python file that is in the same directory as the data directory now the first thing we're going to do here is just import a bunch of modules that we're going to be using so we're going to import the OS module which I believe stands for operating system we're going to import Json which is how we're going to work with Json files we're going to import shutil which is going to allow us to kind of do some copy and overwrite operations we're going to say from sub process import and we're going to import pipe and run now this is going to allow us to run really any terminal command that we want so we can for example compile and run the go code using this and then we are going to import sys which we are going to use to get access to the command line arguments so the very first thing I want to start by doing here if we go back to our project text is we want to find all of the game directories from some directory in this case is called slash data we want to create a new directory some kind of Target directory where we're going to be copying this into so what I want to do is I want to grab from command line arguments what the source directory and the target directory is relative to the current path so that I know where I should be storing kind of all of the games that I find and where I should be looking for those games to make this script a bit dynamic so what is a command line argument well let me just show you down here in the terminal so a command line argument is something like python I pass my uh file name so get game data and then I do something like Hello World okay so hello and world are command line arguments you've seen this before whenever you've ran any commands like when you make a folder mkdir and then you put the folder name the folder name is a command line argument so in this case we might do something like python getgame data then we pass slash data which is going to be our source directory and then we pass something like Slash new games or something like that and that's our output or Target directory so I want to show you how we can parse out those command line arguments so to do that we're going to use the system module so first I'm going to say if underscore underscore name is equal to underscore underscore Main then in here I'm going to grab the command line arguments now the reason I'm using this is because I only want to execute the main script here if I'm running this python file directory directly sorry if I'm not directly running this python file I don't want to be executing the code that's going to run through and do all of our operations otherwise if we try to import the python file and we didn't have this line it would go ahead and run all that code when we don't want that to happen so when you have this here this just checks that you ran the file directly and it won't execute anything in here if you were importing say a function or class or something from this file hopefully that makes sense what I'll do here is say RX is equal to sys dot ARG V okay now just to show you how this works we'll print out args and let's run our code so I'm going to say python getgame data data new data okay hit enter and notice that my command line arguments are first of all the name of the Python file and then I have my two command line arguments so data and new data that I passed here now if you are on Mac you will probably need to run Python 3 getgame data okay so now that we have our arguments I want to make sure that we have a valid number of arguments which in this case is going to be three three because we have the file name and then we have the two extra arguments and we want to make sure we have those two extra arguments so I'm going to go here and say if the Len of args does not equal three then I will raise an exception and here I will say you must pass a source and Target directory only okay so that's fine so now if we run our code here and I only pass for example The Source directory then notice that I get my exception if I pass my target as well then all is good okay so now I want to get my source and Target directory so I'm going to say Source Target is equal to sys Dot argv and I'm going to do one colon like this and actually we can put this as args now the reason I'm doing one is just to strip off the name of our python file which we don't want and just get the two arguments here and then store them in separate variables okay now that we've done that I'm going to create a function and this main function is going to accept our source okay and our Target and I don't know if that's actually the right thing that we want to call it um yeah I guess that makes sense source is where we're looking Target is where we want to put our new directory so that's fine we can leave it like that and then here we'll go and call the main passing our source and Target directory names perfect now that we've done that what we actually need to do here before we go any further is we need to create a complete path from the location where we're running our python file to whatever this directory is so in the case where I pass you know data or Target well I want to get the full qualified path from say like my C drive all the way to this directory so that it's going to work in my program if I just try to use data or I just try to use Target we can get some weird errors depending on what operating system we're running on so what I'm going to do here is say my source underscore path is equal to os.path.joint and the first thing we need here is the current working directory which I can get by using the command I gotta check my cheat sheet here because I always forget oh s dot get CWD okay so I'm going to join the path of CWD and my source directory now you should always use os.path.join when you're trying to create paths don't think you're going to be clever here and have a path like you know C drive colon slash slash and then you add like you're doing a string concatenation don't do that the reason you should not do a string concatenation is because if you're working on different operating systems the path dividers are different when you use os.path.join it automatically will join the path based on the operating system you're working on so you always want to use this don't do string concatenations uh to try to create paths it might work sometimes but it's not kind of a good practice to do that okay so we've created our source path now just with to explain this line here CWD is our current working directory which means the directory we ran this python file from so wherever we ran this python file from that's the working directory so in this case it's going to be users Tim desktop python scripting then whatever over these paths are are going to be relative to that working directory so I combine them together right so now the directory will be this and then it will have my source which would be data right and then the next one will be my target okay so let's copy this and make this target and change this here to be Target okay so now we have our source path and our Target path now what do I need to do next here well I need to find all of the game directories from my source directory so let's write a function that can do that we'll say find all game endures like that we need to know where to look so we'll go source okay I'm going to create a variable up here and this is going to specify what we're looking for in our directory so like what needs to be in their name so in this case I'm going to say Game Dirt pattern is equal to and we'll just call this game which essentially means we're going to look for the string game in any directories uh and that will tell us that we want that directory right now what we're going to do is we're going to essentially walk through the source directory look at all the files and directories and then match any directories that have this game in them um and if we have that then we'll add them to a list we'll return them Etc okay so let's do this I'm going to say my game underscore paths is equal to and this is going to be a uh what do you call it just empty list and actually I want to change this say find all game paths because it's going to give us the full path not just the directory name next I'm going to say for root I believe this is going to be durs files in OS dot walk and I'm going to walk the source path okay now what walk will do is walk recursively through whatever the source directory is that you pass uh to this this os.walk command so it's going to give you the root directory the directories and the files that are contained in the current level that it's walking through now what this will do is recursively continue to look so in this case it'll look in data but then it would after looking in data look inside of blank then look inside of hello world then look inside of rock paper scissors game it would look through all of those directories now we only care about the directories that are immediately inside of data however if you wanted to recursively look through all of them this is what you would use hence why I'm showing it to you here now since we only care about the first kind of top level directory we only really need to run this command one time so at the end of my for Loop I'm just going to put a break so that I only do this once and this is just an easy way for me to grab kind of all of the values one time right so you just break at the end of the for Loop so we only run this one time okay now that we've done this uh we need to Loop through all of the directories so I'm going to say 4 and you should not use dur because that's a built-in command so I'm going to say directory in ders like that and then I want to get the name of my directory and match that against my game dir pattern so what I will do is say if my game dur pattern is in my directory dot lower and I'll put this lowercase just in case we have a capital game or like a capital on the G or something like that you don't have to do this but I think it makes sense to do that if this is the case then I'm going to add this path uh to my game paths so what I will do here is say my path is equal to OS dot path dot join and then I'm going to take my source and I'm going to take my directory like this and I'm going to say game paths dot append my path okay then down here we will return our Game Pass now the reason I'm doing this is because this uh durs list here gives me all of the names of directories not the paths to those directories now here I want the full path the reason I want the full path is so that I can easily copy it later on if I don't have the full path I can still create that full path but it's just easier to return all of the full paths right now so that's why I'm doing it so I'm saying path is equal to os.path.join taking my base path where I know this directory is contained inside of and then I'm joining them together right taking the directory name adding it to the base and then adding this to my array or list of paths and then I will return them fantastic so let's try this out now and make sure that this works so let's say our game underscore pass is equal to find all Game Pass and then we'll pass here the source path it will print our Game Pass okay so let's give this a shot here and see if we get any errors or not so here we're going to pass data that's going to be our source directory so make sure you have data there and then Target will be our Target although it doesn't matter right now all right so notice now that I get my full paths to all of the directories that contain the name game so I have my hello world I have my rock paper scissors game and I have my Simon Says game now notice that I've ignored the blank directory I've ignored any files even if my file had the name game inside of it because I was only looking in directories fantastic okay so that is working the next thing that I want to do here is I want to take all of these paths and I essentially want to just get the new directory name that I want to copy these directories to I know this seems a bit weird but remember what we're doing here is we're taking all of these directories and we're copying them into a new directory but we're removing the game part from that directory name so when I copy you know Simon Says game I'm just going to copy Simon says not the game part into a new directory so I need to get that kind of destination path before I can perform that copy because to do a copy I need a source I need a destination right now we just have the source so we have to create the destination now one thing we also have to do is create the new directory and we can actually create the new directory right now so let's make a new um function here and say create underscore dir and all we'll take for this is the path to the directory that we want to create inside of here I'm first going to check if this directory exists if it exists I don't need to create it if it doesn't exist then I will quit so I'm going to say if not and then this is OS dot uh actually is this going to be I think it's os.path.exist yes okay that's correct so if this does not exist then I will say OS Dot MK dir which stands for make directory and I will make the directory at this path okay that's it for creating the directory so let's go here and let's say Creator and the directory that we want to create is going to be the target path right so we'll just pass the target paths path there sorry okay next we want to get all of those destination paths so I need to make a function that is essentially going to look at this whole path and it's just going to give me the directory name but removing the game aspect now I know this seems a bit backwards how I'm doing it I'm doing it intentionally so I can show you how you strip apart paths and take like certain components of the path so believe me I know there is an easier way to do this but I'm trying to show you like how you can use a lot of different commands here so hopefully you appreciate it anyways let me just quickly look at my cheat sheet here okay uh and I'm gonna say that this is called get name from has I'm going to take in my paths and I'm going to take in what I want to strip off of my directory name so I'll call that two strip I don't really know what else we would call that so I'm going to go here and say new names is equal to an array and I'm going to say four half in paths now what I want to do is I want to split my path into the just the directory and the base part right so like The Parent Directory of just the directory because remember we have the full entire path so when we had Game Pass we have the entire path to the game directory so we're going to split it to just the game directory and then whatever that base path is now the way we do that is we can say underscore because we don't care about the beginning part and then we can say der underscore name is equal to OS dot path dot split and then we can just split the path now this is automatically just going to give you the very last aspect of the path whether it's a directory or a file or whatever it is and then the remaining part of the path on the left side again don't try to get clever and do this Yourself by splitting different uh like slashes or trying to find the PATH divider just use these built-in functions because they handle the different operating systems for you okay so now we have the dur name now what we need to do is remove the game aspect or whatever aspect from this turning so I will say new your name is equal to my dur name dot replace and I'll replace whatever the two strip string is that we pass which will be game with an empty string which we'll just get rid of it from the uh the directory then I'll say new names dot append my new dur name then I will return my new names fantastic okay so now let's get our new names so let's say new game dirs is equal to get name from paths and our paths is going to be the game paths and to strip we're just going to strip off the string game fantastic now let's print our new game dirts all right now notice I have create directory as well so we should see that a new directory gets created here when I run this let's try it and see what we get and notice now that we get Hello World rock paper scissors Simon Says plus the target directory was created so I'll delete that so we can create again later okay now we do have the underscore here now that makes sense right because we're just looking for a game now in this case we can look for underscore game and we can remove that entire thing but it's up to you how you want to do this right if you look for underscore game then if you started the directory name with just game it wouldn't count or it wouldn't remove it so you could do some Advanced logic to remove this if you want but for now we'll just make it underscore game I think you guys know what I'm saying here okay so let's clear that and what do we need to do next well now that we have our kind of destination our source directories for all the games as well as where we want to put these games now we need to well we need to write them right like we need to actually do the copy operation so what I'm going to do is make a new directory or new function not directory and I'm going to call this copy okay now what copy is going to do is take a source and a destination and it's going to copy the source into the destination now I will actually um kind of change this a little bit and I will say copy and overwrite now the reason for this is that if the directory already exists I want to overwrite it and I will show you how we can do that now keep in mind the commands I'm going to use here are just one option to do this there's like a million ways you can go about copying and deleting directories but I'm going to show you how we delete a directory if it already exists and then of course how we copy it and this will be a recursive copy which will copy everything so if there was another directory inside it would copy that directory as well and all of the content tense of that directory a lot of the copy operations don't do that so just keep in mind if you want a recursive copy you probably want to use the option I'm showing you here so I'm going to say if os.path.exist destination that I need to start by removing the destination folder which I'm going to use my shutil for so I'm going to say shutil dot RM tree which stands for remove tree which is essentially a recursive delete and this is going to be the destination now after this I'll say s h util dot copy tree and then I will copy the source to the destination all right that's all good that's all we need for copying and overwriting or overriding however you wanna I think overwrite is the correct term there okay so now that we've done that we can use this function so we need to essentially Loop through all of the different paths that we have so the source paths as well as the destination paths and then we need to run this copy command so I'm going to say four and we'll call this uh SRC destination in I'm going to use this ZIP function I'll explain how this works in one second but I'm going to pass my game pass and my new game directories I'm going to start by saying my destination path is equal to OS dot path dot join and I'm going to join my target path with my destination folder name then I will run the copy and overwrite function with my SRC and my destination okay so let me explain how zip Works what zip will do is it will take matching elements from two arrays and combine them into a tuple which then gives me access to them at the same time so notice right these arrays or these lists are going to be the same length so this first one is going to be all of the original game paths and this is going to be our new game directory names that have this underscore game removed now I want to be getting the game path associated with the new game directory name because I want to copy whatever's here into this new directory name so I zip them together so to show you a quick example if we have one 2 3 and we have like a b C then if you zip these together what you're going to get is an array that has one and a or a tuple sorry that has one in a 2 and B Etc it just combines them together so the matching elements at their indices get kind of matched up and that's why I can say Source destination here and iterate through the zip okay so now this should actually be good however I need to make sure that I create this directory before I do this otherwise we're going to get an error where it says our path doesn't exist okay so let me slow down for one sec I'll explain what we've done we can run it and we can continue so what we've done here right is we've gotten the source path as well as the directory path we have found all of the game paths that we're interested in so essentially those directories from The Source directory we have then found what we want our new game directory names to be we have then created the target directory where we want to copy all of these into and now we are actually performing that copy operation okay so we're at that point so let's run this and see how it works so python get game data and then we're passing data and Target I hit enter and when I look in Target here you can see I now have Hello World rock paper scissors and Simon Says with the contents of those directories because I just copied them in perfect so now let me delete this and we can continue so the next thing I want to do is write a Json file that just has some metadata about the different games so what the game directory names are and how many of them there are this is fairly straightforward so let me create a function I'll say make Json meta data file and here I will just take my path so what do I want the file to be or where do I want it to be and then I will take my game durs okay now here I'm going to say that my data this is the data I want to write into the Json file is going to be equal to oh I will say my game names is simply my game directories and I will say number of games and this will be the Len of my Gamers very straightforward not the most useful data but just want to show you how we write Json so now there's a few ways to do this but the preferred method is to do the following I'm going to say with open half in wmode W stands for right this will override the file if it already exists if you wanted to read the file you'd use R so I want to write and override if it exists I'm using W and I'm going to say as F and then I'm going to say Json dot dump and I'm going to dump my data into my file now if you've ever seen Json be used before with dump s dump s stands for dump string and that's going to dump the uh Json data you have into a string we don't want that we want to dump it into a file or just really save it into a file so we pass the data that we want to save which in this case is a python dictionary which can kind of be represented as a Json object and we want to save it into this file object that I'm opening using something known as a context manager now the reason I use this is because as soon as we're outside of this width we are automatically going to have the file closed for us if we didn't use with we would have to manually close the file and there can be some problems doing that especially if an error occurs before we're able to close the file we can have kind of a memory leak going on so it's just preferred to use this it's referred to as a context manager that just makes sure your cleanup operations happen no matter what essentially okay so we have this uh Json metadata file so let me copy this function let's paste it here we'll say make Json metadata file and now we need to pass a uh path that we want to use so I'll say Json underscore path is equal to OS Dot half dot join and I will join my target path with my metadata.json name so I've just come up with this name you can name it anything you want and I will pass here the Json path and my new game durs right there we go okay so let's run this again and see if it creates the Json file okay go to Target and notice we now have our Json file if I save a little Auto format here and you can see we have our game names and our number of games fantastic okay so let me delete that all right now that we have done that I need to show you how we can compile this go code and how we can run the go code alright so let's write a function here that will allow us to compile the code inside of a directory now let me just space up my functions here so a bit easier to read okay and let's do it here so Define this will be compile and a game code now here we'll take a path which will be the path to the directory we want to compile the code inside of now what we need to do here is we need to locate or determine the name of the file that we want to compile you know no pun intended or rhyme intended there but you know what I mean so we have main we have code we have file now in this case we get hard code that but we don't want to do that we want to have this Dynamics so it works with kind of any go file no matter what the name is so essentially we're going to look through the directories of our games and we're going to search for any file that ends in dot go right so let's create a variable here and say game code extension and I'll just make this dot go so that's what we'll search for now if we find that uh then we'll compile that code now we could compile all of the files that have that name or we could compile just the first one in this case I'll just do the first one because we're assuming we only have a single code file in each of our directories all right so let's say code file name is equal to none and then let's walk through this directory like we did before so we're going to say root durs files in OS dot walk our path and this time we're only interested in the files so I'm going to say four file in files and let's not forget to break here at the end of our for Loop so we only do this one time not recursively okay so four files in files I'm going to say if the code what did I even call this the well game code okay game code extension if the game code extension is in the file then I will say my code Sue that's my code file name is equal to file and then I will break okay that's actually all we need I know it went fast there but that's all we need to determine what the name of our code file is so we Loop through all of our files here right so for file and file and we say okay if this dot go extension is associated with the file then we'll say code file name is equal to file uh and then we'll break which means we'll just find the first one we don't need to continue looking now what I just realized though is that this could be a little flawed because I could technically have a file that's like dot go dot Pi I know that would be a weird name but that wouldn't be a DOT go file that would be a python file right well I think that would be a python file but it would be a bit weird either way if we had something like that so what I'm going to do instead is use a little bit of a better way of doing this I'm going to say if file dot ends with so I'm going to check if this is at the end of the file I think that's a better way to do this check okay perfect so now what I'm going to do is I'm just going to make sure that we did have a code file if we don't have one I'm going to return if we do have one then I'm going to run the command to compile this so I'm going to say if the code file name is none then just return just so that we don't get an error when we try to compile a file that doesn't exist now we need to actually compile the file so the first thing I'm going to do is say my command is equal to and then I'm going to make a variable up here which will be my compile command so I'm going to say game compile commit now the way you write these commands is just in an array of I keep saying array and in a list of strings that represent the commands that you need to run so it's going to be go and then build so this is the command go build then whatever the name of the file is so this is my like base command so I'm going to say my command is equal to my game compile commands plus and then I'm going to put another list here and I'm going to add inside of here the code file name now what this will do is make the command B go build and then whatever the file name is then I can pass this to a function that I'm going to write which will just run any command that I pass it so we're going to say run command and we do actually need the path that we want to be running this command from okay so now I'm going to say that our current working directory is equal to OS dot get CWD is that is that what it is get CWD yes that's correct and I'm going to show you how we change our working directory into this path the reason for that is that I need to run this command well I don't need to but I'm going to run this command directly in the directory where our game code is so rather than being in the directory or my python script was run I'm going to change into that directory run the command and then change back to a different directory just to show you how you do that so I'm going to say OS CH dir which stands for change directory I'm going to change into the path directory then I am going to go and run the command now to run the command we're going to use the sub process command run which I believe I imported from up here yes I did so we're going to say run and we're going to run our Command and we're going to say our standard output is equal to pipe and our standard input is equal to pipe and I'm going to say Universal new lines is equal to true now this is where the command gets a little bit complicated right um you know what does all this mean well the standard output and the standard input is the location where the command is well accepting input and spitting out the output now pipe is kind of this special thing that we can use that makes a bridge between our python code and the process that we're using to run this command now I'm not really going to explain it more than that but just understand that that's why we're putting it here it essentially allows us to communicate between the process that can actually run this special command because this command is not a python built-in Command right like this is a command we just run in our terminal or in our command prompt again you don't have to understand it maybe that explanation was good maybe it wasn't but this is how you do it you import run and pipe from sub process you pass the command again the command is just going to be an array of all of the different words that are in that command or a list of all of the different commands and then if you wanted the results uh it will be given to you in this result variable so you could print out the result you could read it in as a Json object blah blah you could do whatever you want and you could see what that result is and in fact we'll print out the result uh just so you can see what it looks like so I'll say compile result like that okay now the last thing we'll do just as good practice is we're going to change our directory back to the current working directory we were in before we ran or before we change directories into this path just to make sure when we run this command another time uh we don't get any potential errors okay so we got the working directory where we're in changed into this path directory ran the command and then we went back into whatever the previous directory was just that we don't have any potential problems here now we need to call this function so we'll say run command and command and path that's actually fine we can just pass those values great so now where do we call our compile game code function well we're going to call that from inside of our copy here so I'm going to say compile game code and I will compile game code in the destination path because once we've done the copy and overwrite once this is finished now that directory exists let's compile the game code then move on to the next directories so in this point the project is pretty much finished let's run it though make sure it works okay compile result compile result and notice it gives you kind of this like result object right I'm not going to uh again kind of dig into exactly how this works but you can see that if you wanted to look at the standard out then it would be STD out from this compiled process or sorry not compiled completed process and if this had any result like if there was an error you'd be able to see it there and the return code would most likely be an error return code as well now let's look at Target here so we have Target we have hello world and now notice inside of my directories I have my compiled code right I have main.exe code.exe file.exe Etc if you were on Mac or Linux you would get I believe a DOT sh file uh or an executable file on your operating system it'll be different than if you were on Windows okay so I think I'm gonna wrap up the video here all of this code will be available on GitHub I'll also zoom out a bit and kind of slowly scroll through in case any of you want to pause the video and have a look at any of the lines here but I thought this was a useful project just to teach you some basic kind of automation I don't want to call it automation scripting um syntax tools commands you can use in Python I write code like this all the time in Python it's really helpful this is kind of my go-to language when I need to do like file or file system operations so hopefully you guys found some value from this and of course if you did make sure to leave a like subscribe to the channel you guys can check out my programming expert course if you want to learn how to become a python programmer or a software engineer as fast as possible and I look forward to seeing you in another YouTube video [Music]
Info
Channel: Tech With Tim
Views: 144,913
Rating: undefined out of 5
Keywords: tech with tim, learn python, python, python tutorial, python projects, python programming, learn python programming, python for beginners, python tutorial for beginners, python course, how to learn python, python tutorial project, learn python with 1 project, getting started with python, python beginner projects, python projects for beginners, python programming tutorial, python crash course
Id: dQlw1Cdd3pw
Channel Id: undefined
Length: 42min 6sec (2526 seconds)
Published: Mon Nov 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.