Python SharePoint Download File - Update

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what up y'all i am lou and uh it's been a while man it's been a while i actually been extremely busy um with just what's my business you know a lot of new projects you know expanding growing which is good um also just kind of um my wife just had a baby not that long ago but you know been four weeks now so been helping out with baby as well so i've been busy with that let's just say getting very very little sleep nevertheless so i do enjoy doing youtube and you know i gotta squeeze time into it right so that's what i'm doing now gonna try to squeeze in much time i know some of y'all have emailed me um even some of the comments that you have put on youtube and again uh i'm trying to catch up i've been a little bit behind with everything especially youtube related so if i responded back to you it yet i will be responding back soon sooner rather than later but i just been real busy man we're like extremely busy with everything uh but i will be making time so haven't forgot about y'all so this could be a follow-up video from a previous video that i made which is um downloads files from sharepoint using python so we take a look at it it's this guy here so if before i go any further i'll much appreciative you give me a thumbs up like this video follow me again i know i've been kind of slacking a little bit but um i do i am going to be making i am making time um to making more youtube videos i want to be able to help you all out in any way possible so i appreciate you know the follow and the like you know whatever i could do to help me out man that that's awesome um so it says download files from sharepoint you the python video so i gotta i received a lot of uh requests of hey you know the video shows how to download a specific file well what if i want to download all the files right from a from a folder sharepoint or what if i want to download a specific pattern of files so let's say you may maybe you have a folder and it has files in there for sales reps maybe in the file name it says sales reports now you have other files that says territory reports or whatever right i'm just using an example you may not want to download all you just want to download cells reports only well i'm going to go through a video kind of showing those two so we're going to i already have a video on how to download a specific file from sharepoint but now i'm going to show you how to download all files from sharepoint then how do you download by pattern are you looking for a specific pattern we want to download those files only so we're not gonna start from scratch we're gonna we're gonna actually use this the source code from that other video and then we're gonna just you know enhance it pretty much right so let's go ahead and get started so this if y'all have not seen it seen the video yet you know you may not the source code may be new to you so recommend i'm not going to go over detail to what the source code source code is doing go and look at the video you can also download the um the source code from uh the github account so you can do that before we actually get started one of the things i'm going to do let me create an another branch um so let me create a branch real quick and i'm going to call this i'm going to call it update and i'll put the year in this case where what are we 20 21 10 16. all right so now that i create a new branch let me just go ahead and change to the branch update 20 1 10 16 okay so we're going to make all the changes in this one so again though you could search about by branch to see the original source code and then of course the new source code that we're about to make um so let me go to my config file one of the changes that i'm doing differently here is i'm going to remove this document parameter that we had and it's just going to be shared documents so the reason why is if we go back to the original pass here that will only specify in the folder because i was already specifying in my config file the root pass of the folder so kind of give an example if we take a look at it see how the folder is say 2020 so let me go to this is the sharepoint site and you could tell i'm in the 2020 folder but the root folder is actually youtube so instead of kind of putting that into the config file i'm going to take it out you could just specify it in your um in your as as a parameter and we're going to go through that it just makes it more dynamic instead of being hard-coded in so that's really the main reason why so i'm going to remove that piece out the way the rest will stay the same right you're gonna replace this with your email everything even the curly brackets because i think some of y'all may not be replacing the curly brackets so take away the curly brackets as well and put in your password same thing for your domain and then of course your site name so in my case my site name is called the level mint right there it goes right there if you're not sure what it is you look at your url you can get get all your information here's my domain and my site name all right so but this is one change just remove that piece that was one of those things that we needed to remove now we're going to make some changes here in our sharepoint file so we're going to start off there first um so let's go ahead and make some of those changes uh let's see one of the things that we're going to be adding on here is so see how see how we have this we have this create folder connect folder then we have a download file again it's very specific we're downloading one file so now what we're going to end up doing we're going to create one that's called a get file list so let's go ahead and do that we're going to be good files list um in this case it would be the folder name so we're specifying folder name in this case um it will be we're gonna we're gonna now um connect to our folder instance uh folder name and then we're gonna do a return and in this case we are returning files okay so this this object here it's part of the sharepoint class which would be under this folder uh class and then this is one of the objects ultimately just gives back a list of all the files that's what it's doing here right so that's number one just by providing the file name we're gonna get back a list of files in a json format so just fyi that's that's how that's the format that we're going to get it in the next function that we're going to create we're going to call it download files right now fiopa files because we're going to download multiple files that's what we're trying to do here same thing we're only going to pass in the folder name you know because again we're downloading multiple files there's no need to provide a file name in this case um in this case what we're going to end up doing is let's call this files list and we're going to end up calling the get files function um get files function and we're going to provide the folder name and then of course we're going to return this object back so what we did here is we created two functions uh download files and then get filed list now this get file list is never really gonna be and i probably should this is what i should have done it's not gonna be called outside of this uh class it's something that you're going to be called within the class only so [Music] but the the function that we actually want to call in our in our project file is going to be the download files which again would ultimately it ends up calling this get files list object over here to get to return that back um i'm trying to think so this is it these are the only two things that we're changing again you could tell over here on the side this is the only thing that we added so from the original project that we had we just added these two new functions um to our sharepoint class and that's it dude we're done with this so next we are going to we're pretty much going to scrap all of this there's a few things that we're going to do slightly different again when i did this it was just more than an example i'm gonna kind of build it out in a better way but we're also gonna add that functionality to again download a file multiple files or pattern files so that's kind of what we're gonna do with so [Music] let's see so some one of the two things that we're going to end up importing is going to be our regular expressions um package if you're not familiar with the regular expressions go take you know just google it kind of get familiar with it you don't have to be an expert at it but it's very powerful so i recommend you know getting familiar with it at least the basics of it right they want to bring in our system package all right so all of this here that we kind of hard coded in in here we're going to get rid of we don't want what we're going to end up doing is we're going to replace it with these arguments all right let me drink some water man my throat is getting getting very raspy i live in texas and man at nepalin's pretty bad and my allergies have been kicking my ass the last couple of days and my throat is kind of raspy especially now that i'm against talking a lot but either way let's get let's get going so what we're doing here is um we we have a few arguments uh we have total of four arguments that we're passing in we got a folder name um we have a folder destination we have a file name and then a file name pattern so those are the arguments when that we're going to end up passing in whenever um whenever we run our python file on on the terminal so again if if you're not familiar with system arguments uh i can make a video and be more than happy it's really not that complicated but ultimately think about just passing it parameters when you run your python file so whenever you do your python then you put in this case project.py like to run the file and then in the terminal you can pass in arguments as well that's what we're doing here you know um one two three and four now our the arguments actually start on zero and that's for something else um that that is already being utilized by default so we got to start on the next the next um argument which is again one two three and four so that's kind of what we're doing here so let's see so next what we're going to end up doing is we're going to create uh four functions here right so let's the first one it's going to be called get file and in this case we're going to provide file name and folder name all right so that's what we're going to do uh file name folder name let me just let me just let's get them get them all created real quick so that's number one the other the second one is gonna be get files and this would be folder name only um [Music] yeah no that's not right so this will be uh let's see this would be folder folder name let me just call it folder all right uh the third one would be get files by pattern so in this case we're going to provide the pattern and then the the folder oops um and the last one is going to be save file the save file we're going to provide the file name and then the file object that we need to pass in again when we save it we're getting an object and so we're going to end up saving that object and then of course the the file we're going to provide a file name whether it be the original file name that's in sharepoint or you created change it to another file name but either way that's what we're doing here um this is for the save so let's do the same first save um function first so in this case we're going to be doing a file directory pass [Music] and we're going to provide the folder destination and then the file name because that's what we're passing in right so the file name is is a parameter that's past being passed in um let's see that's not what i wanted so let me call this i don't want these to overlap um that's fine you know what that is let me change it i don't like that um you call it file in for name all right that's fine um okay so we're again our whatever our folder destination is then of course whatever the file name is going to be which is based on what we pass in so just kind of going back and why i changed it even if i don't change it it's still fine i just hate having objects that are being created like in advance here and then also the same file name being utilized inside the way the scoping works again they're like kind of in javascript and other ones this would actually override this anyways so that's what's without an issue i just hate having the same fight the same names i'm not i'm not i'm just not a big fan of it that just me i guess so let's see so next we're going to do a with open which is the um file object not the file object what am i doing this would end up being my bad the file pass and this would be a write binary as f for file then we're gonna do right we're gonna write the object that's what's actually gonna get saved right in the file gonna be the actual object and then we're gonna do a um a close we're gonna close it out so we're specifying the path which the path contains the again the the folder and the file name you know together and then we're going to actually write whatever object it is right like if it's a pdf or an excel or a word whatever it is whatever that object is we're going to write that data to that file and then we're going to close it so that's pretty much what we're doing here right um so next let's do the first piece get file you know right passing in a file name and a folder so let's go ahead and do that so let's call it file object um equals sharepoint download file by file name and folder yep and then once we get that object back what we're gonna do we're gonna call that save file function that we created um [Music] we're gonna pass in the file name and then the file object and ultimately it's going to save because that's what this is doing it's going to provide a destination save it so on boom done deal so now we're going to do the get files right so we want to get mini files so the first thing we're going to do here is files list this will be sharepoint download files right now fio pa fios because and that only requires the folder to be passed in which is what we got here the folder uh folder name um yes and then what we're going to do here we're going to do a for loop it's going to be file and file the list and then from here what we're actually going to do is hold on why is this not registering uh let me try to think hmm what's happening here oh i got it i just just because of this runs first and that either way it's let me just move to the front it shouldn't be an issue but let me just move to the front all right so under get filed we're getting our list then we're going to do an iteration right then once then what we're going to do next is going to be we could call the get file [Music] function on top and we're going to pass in the file the file name so just fyi this list that we get back it's a um and i'll do i could do a print statement so we'll take a look at it it's a json object that we're getting back so we're ultimately going to iterate over that list uh there's a list inside we're going to iterate through that and then inside there we're going to get there they they're the key value call name which is the file name and that's what we're getting back out so that's why you see me calling this file and the name in it we're going to retrieve that file name all right and then uh let's see then i need to specify the folder that's what it is [Music] so what i'm doing here is i'm saying down download all get the list of all files in this one folder right once i do that i'm saying all right let's iterate over that list one by one and i iterate over that list one by one then i'm gonna call the um all right let's i'm gonna call this function which requires a folder then the file name so i got a file name and a folder right it's kind of what i'm doing here and this should ultimately run this which is going to be download that one file and then save it that's what it should do um the last function is going to be get filed by pattern all right so what we're gonna do here is this one's gonna be very similar to the get files so let's go ahead and copy this guy where we got a folder again we're going to download all the files which is what we're doing here the difference is we're going to now use our regular expressions to say search by pattern which in this case would be called what file name pattern and then we're gonna search the file name so again if you have a file name that's called sales reports and the pattern is the word sales if it had the match then ultimately what it's going to do it's going to run this function which is the get files get file my bad now files but file function which is again is going to provide the file name and then the folder by doing so it should go it should be able to go ahead and save the file as well you know get that object in return then save the file um [Music] okay so i'm thinking that's it here i'm going to delete all of this we don't need this so now what i'm going to do i'm going to add just some conditioning logic down here so i'm going to call it if file name is none it's not none then we want to run the get files function which is going to require us to provide a file name and a folder name which is again part of these two arguments up here right uh the next one is is file um name pattern is not none we're going to get the get filed by pattern and our argument here is hold up i just realized i'm not using the right pattern i need to that's fine yep all right so then i'm gonna say get files by file name pattern um and folders name yep folder name then the last one will be else and get files again files being get everything and i'm going to provide the folder name for that parameter um so i think this solves for everything so we have a function to save the file we have a function to get one file only we have another function to get multi get all the files from one folder and then we have another function to get filed by a pattern again specific pattern so i think that's it guys so the only thing i need to do on my side here is a my config file i'm going to put in my information so again i'm not going to show it so let me not share that screen real quick let me put that in once i put that information and then i can run it on my side okay so now that i have that information in there what we're gonna do now is let's see okay so [Music] what i'm going to do i think i have a folder in my download folder that i could put that in let's see let me find it real quick yup this guy and i'll show you in a minute so [Music] run project right this is my project file um the first argument is folder name so in order to determine which folder name i'm going to provide let me go to my sharepoint site in this case it's going to be youtube 2020 alright so that's my folder name yeah youtube 2020. so i'm going to do [Music] youtube forward slash 2020. all right i'm going to put a space my second argument is going to be the destinations like where do i want to put my file into you know most likely you're not going to want your file to drop into the same folder your project they're just not realistic you want to put them somewhere so in my case again double quotes put the file the folder that you wanted to go to then you know close it out all right so that's my second argument my third one is gonna be file name so let's say for now i don't want to get other files like i don't want to get i don't want to get one specific file i want to get all the files so i'm going to type the word none because we have logic down here that says if none you know if not none then run that so i just put none for now then the last one is pattern if i want to find something by pattern for now i'm going to say none because i don't want to download my pattern i do want to download all the files so if we take a look at sharepoint we have two files in here right two files um and i'm gonna look at the folder this is a folder that the file should flow into okay so all the file that we downloaded should come in here so let's go ahead and run it and see if it works cross fingers damn it didn't work let's see what happened um there you go see i put my forward slash already so i don't need to do that so what i'm going to do here is remove this and just leave it blank just like that so now let's try again uh before i try let me open up the folder as well all right so now let's try boom there goes two files so as you could tell we downloaded loaded both files that existed in you know in sharepoint so which is good it did what we wanted to do right we want to download all the files that's exactly what it did we just provided the directory pass again in my case youtube 2020 if i go to my sharepoint youtube 2020 that's what you would have to provide okay so now let's let me delete this and we're gonna do it by file not by pattern but by file so let's say if i wanna look for i have a file i think we'll call sample pdf now let's try that let's see what we get should only get one file i did not get one file let me make sure the right argument let's go to project file name with argument three i think i know what happened i put double quotes but i don't think it needs double quotes for this um yeah that shouldn't matter so what let me make sure demand that i that is the name i did spell your writing right yeah i spelled it right that's not wrong um sample pdf sample pdf that's all on some of that gets all right so something's let me go back to my project what are we doing here so when we're doing a get file we gotta provide a file name and then a folder let me look let's look at this side by side so [Music] file name get file yup folder yup that looks fine download file [Music] should be executing this inside and get file just want to make sure yep inside get filed so it isn't it is running i'm trying to it almost feels like so it is running this it should be getting the object back by file name folder that's fine and then we're going to save a file name and by object this is not an issue because it saved all the other ones that we had before so this is not an issue has to be somewhere somewhat with this but what the hell [Music] i kind of see what it's doing in the url see what's a document sample pdf sample pdf so i'm trying to think why is it am i passing in oh is that it i passed in file name twice what the hell man it should have been folder there you go that's a that's uh that'll that's what i get damn it okay yep all right let me put it back it should be good now it should be good at least we figured out what the issue was it was just me typing in the wrong name for my um parameter so let's let's run it again and now we should get a there we go boom sample file let me run it again since i know we didn't see it but so now it should only download that one file and there you go we got that one file so the next the next test is going to be well let's download something with a pattern right so i have these files in let me grab a few of these files and i'm going to upload it and these all have a specific pattern this rts pattern right there's other patterns i guess 2021 if you want to do it by year just by the five the rts i want to do rts in my case so let me go ahead and delete this guy so now what we're going to do is for the file name it's going to be none and then for the pattern is going to be rts because that's the pattern right the file name so now let's run let's see what we get um there you go see it's downloading all the rts files only and there you go so we pretty much ran through these examples and you could tell all three of them work file per file all files or by pattern of a file like a file name so either way it works so now the code base that we have um it's driven through arguments as you could tell you know you would have to specify here which sharepoint pass you want to go into which folder you want to save the files into if the third argument is are you downloading a specific file and if you are provide the file name if you're not then put in the word none and then the fourth argument is pattern if you're downloading my pattern put the pattern name whatever that is if you're not if you're going to download all files just make sure to type in none and it'll just download all the files so now you kind of control it to the to the terminal of how you want you know how you want to download your files the reason why i decided to do it this way for this this um example a i want to show you something a little bit different from using the arguments kind of drive what action to take b now if you really are scheduling something where you're like hey i need these files uh downloaded every day uh for example um like some kind of schedule you could use a task scheduler like what's in windows right there's it's a task scheduler you could just create a task and by doing so this is a whole different video if i mean i could create a video over this if you want but it's it's really not to um it's pretty straightforward but ultimately you could create a you provide the the python um interpreter your python.exe if you're using a virtual environment you got to provide the one that belongs to that virtual environment then ultimately you're passing your arguments the arguments here would be what what i had right which was what you youtube [Music] 2020 then it will be your your seed you know download you know whatever the file pass is and then of course it would be like none none or whatever it is right you will provide all of that in there save it schedule it to run every day every week whatever it is then of course you can just have it run when you want it to run um i i for for my company we use this tool called visual chromes it's a a super version of this like test schedule very straightforward visual chrome pretty much does this and way way much more um you know i could connect to databases call procedures different steps like create you know execute this procedure or do x y or z or um i have you know uh call command lines um to run certain code or commands um you know ftp sftp the list goes on and on of all kinds of stuff you could do with it so that's kind of what we use our for my company to uh schedule and automate jobs if you want if you're interested i'd be more than happy to show you that tool but um yeah again for something basic you're using windows easily use this create that in there and man you're good to go um i'm gonna push this code over to uh the github account so again you're more than i am lou cody more than welcome go in there you'll see a new branch in there which is this update 2021 1016 you can look at that source code um with the new changes i i hope this helps out man i mean i know i got requests for hey how do we download all files or how we download follow up our pattern so i'm a whole i'm really hoping that this video you know answer these questions to you and anybody that's new you're trying to figure out how to do something that now there's a code base you just go ahead and download and start using it right off the bat and i'm hopeful hopefully it helps out helps you out um again man i know i've been stacking a little bit on my making more content the way i like i know i've been super busy with my business and having a new baby and all that stuff but i do plan on making some more videos um send me your email send me your message man follow me any whatever y'all give me a thumbs up that's all i ask man that's the only thing i ask i'm not i'm not asking any kind of donations or money or nothing i'm doing very well my business is doing well that's not what i'm what i'm seeking from none of y'all only thing i'm i would appreciate is a thumbs up and a follow and man i am good with that um let me know what y'all think let me know there's other videos you want me to make um and i'd be more than happy when i got the time to squeeze it in do you plan on making some more videos again hopefully this helps and y'all have a great day man peace
Info
Channel: I am Lu
Views: 8,024
Rating: undefined out of 5
Keywords:
Id: -4WKTTlAkz8
Channel Id: undefined
Length: 41min 9sec (2469 seconds)
Published: Sun Oct 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.