Python Download Latest File from SharePoint Using Office365 Rest Package Part 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what up guys what up what up i am blue hopefully guys are doing good today and today's video is part of a multi-part series video of my um sharepoint you know video of doing different things with sharepoint right uploading downloading downloading lists uploading large files so things of that nature right so this is i believe it will be part five we're going to be talking about how do we download the latest file in a sharepoint folder right so you have a folder that folder could have a hundred files uh maybe you for example you have a daily sales file being dropped in there right so every day there's a new file but let's say we want to download uh the latest file whatever the latest file happens to be so that's what we're going to work on today we're going to build a function to download the latest file but before we get started guys if you hit that like button give me a follow i appreciate that being a lot of love a lot of support again hit that like button guys alright guys let's get started so the first thing we need to do is let's go to our office 365 api and we're gonna create and make it a little bit bigger just to make sure that y'all could see it good on your side um we are going to create a new function in this new function we are going to call uh download um what i call a download latest file right and the only argument that this is going to take is going to be the folder name again the purpose of this is because we don't know what the latest file is we don't know the name right so it's not like this function over here where we need to pass in the name we don't know what the name is so because we don't know what the name we just need to provide the folder of which file that we want to download right the latest file from so in here we're gonna end up um importing in uh you know what let's do this out here so let me go ahead and bring in the uh date time uh medicine bring that bad boy in all right cool so one of the things that we're gonna do and i'll explain to you why but we need to create a um since there's no true way um to be able to download the latest file from sharepoint like we cannot call the api and say hey give me the latest file you know that's not how that actually works so what we're going to end up doing we're going to be pulling some metadata of that folder in sharepoint and the metadata is going to include like file name and also uh time like la la what is it called i think it's called like um uh last modified date or something like that but either way those are two two properties that we could access right as we enter we get a list of all the files and from there we're going to iterate over everything and we'll be able to determine the file name and also when it will last modify like the newest date what we're going to do we're going to end up putting that into a dictionary and then once we do that we're going to do a sort and now we sort it we're going to order by time and then from there we'll be able to get what is the newest file of the list of files so again there's not a true way to do it through the api but we're gonna download the metadata the the list details of the folder we're not downloading the objects like the actual content we're just downloading the um the metadata was in the folder which would give us again file name that kind of stuff right uh modified shareable links you know properties of every every file we're going to put that into a dictionary sort it and then ultimately get the first index of it which would be the newest file so that's kind of what we're doing here but because we're getting text values back and like the date value it's in the text format we need to convert that to a a date object so that's where i'm specifying my format here let me call it date format right so in this case we would it would be um the year m for months the months um d for date uh t and then we would have our time percent h then it would be percent m um and then it would be um what are the percent um s for second and then of course capital z at the end that is a format so again i know this format because i when i did my testing i printed out and took a look at it it's in that format so i need to specify the format that matches the string that we're gonna be getting that's ultimately what i'm doing here uh the next thing i'm gonna do is i need to get a list of all the files in that sharepoint folder so i do have this function over here that does that so i'm going to end up calling that i'm going to assign it to an object called file list and then ultimately i would call that git files list function then of course i'm going to provide my file name my folder name right because that's one of the arguments for folder name ultimately this is going to give me back you know a list of all the files and of course all the properties associated with those files let me go ahead and create this empty dictionary that's where we're going to be populating our file name and then also our date into into that dictionary and then now let's go ahead and do we're going to do an iteration right so in this case it would be file list again this guy we're gonna iterate over let me bring it up um the first thing that we're gonna do is i'm gonna call the date time object and this is where i'm going to call my date time date time um the strip time i think yeah it is this one um because what we're doing here is we're going to be pretty much specified the string right in this case it's file and one of the properties inside of is called time last modified and then i'm going to specify the format so in this case it would be a date format so again we're providing the string and ultimate providing the format which is up here and what that's going to do is going to give it back a date time object a daytime python object right so keep that in mind uh the next thing we need to now append all of these these values the file name and the object to the dictionary so let's go ahead and do that as well i'm gonna call file dictionary and then my key is going to be file dot name because again that's the property there's a property in the call name which is the file name and then ultimately i'm going to assign my date time object that would be my value right my file name is my key date time is my is my value uh once i'm done with that now we're going to have to sort so they just kind of sort dictionary object to get the latest um latest file so that's pretty much what we're gonna be doing next right so in this case i would do i'm gonna call this file dictionary sorted so what i'm doing here i'm sorting it out first right so this would be my key value for key comma value in sort it now in here i'm going to specify my file dictionary dot items okay comma my key would be it would be my lambda i'm going to apply function um which would be item index one which is again this is my value comma and then it would be um reverse equals true because i want to reverse it as well so pretty much this is is going to give us a sorted dictionary and we're sorting it by the date and time because remember my value here my date time object which is my date and time this is the format that's my value and that's what i'm doing here we're sorting it you know in reverse order and it will give us a new dictionary where the latest date time would be the first index index zero it's pretty much what's happening here okay so the next thing that we're going to do here is i want to get the latest file and the way to do that is i'm going to apply the next again so when you apply next this is getting the next value of of your iteration like your dictionary or your list um and here we're gonna i'm gonna call the iterate as well and then i'm passing in my file dictionary sort and ultimately this is going to give me back is going to be back my value my the latest value of my um sorted dictionary right so just kind of keep that in mind um and then finally i i need to download my contents so just like the way we do up here on top right when we call this download file the end result is give us back the file contents you know of what we're trying to download which is this file and this folder but we're going to do the same thing here so in order to do that i'm going to call this context equals download file and it's going to take in two arguments right the latest file name which is again this guy here and then also i need to provide the folder which would be my argument that's coming from up here this is going to give me back same thing this my file contents right and then i could do a return and i'm returning back two values back because i don't know what the latest file number is file name is over here i do because i provided it down here i don't know what it is so i do want to return back the latest file name um that's pretty much what it is right i probably should have made the latest file name because that's really what it is so latest file name and then it would be my contents it's gonna get returned back and that's it guys that's all we're done here with this uh this function which is download latest file uh the next thing that we're gonna do is let's create a new file we're going to call this download latest file dot py this is just more of the of the sample you know just kind of um test it out and see it in action right so let's go ahead and bring in um from office 365 api import import sharepoint um import system and then from pass lib import uh purepass okay so we're gonna have two arguments um so argument number one would be share point folder name so we're gonna call this folder name and this would be index one because remember index zero is your actual python file and then anything after that it could be your additional arguments um argument two it's going to be um location or remote a remote folder destination oops and here we're going to bring in folder we're going to call this folder dest this is ultimately going to be where do you want to save this file to right we're downloading the file where you want to save it to so we're downloading the latest file we want to save it somewhere right in this case it'll be somewhere locally but it could be a remote location as well all right guys so so the first function that we're going to create here is going to be our save file function so let's go ahead and write that up it will be save file this is going to take in uh three arguments so be the file name the file object and then also the folder destination you know where we're going to save it to uh the next thing is we need to provide the file directory pass so again this is like the full pass on um the root pass and the in the file name pretty much you know we're just concatenating that together right so be our folder destination and uh file name next we're going to end up creating the the object right on what we're going to save so this is what we're going to provide our file directory pass it's going to be a write binary and then we're going to call this f for file and then last we're going to do a file write and then of course we're going to write the object which is one of our parameters over here right we're writing that so this pretty much wraps up our save file function all right the next thing that we're going to do is we're going to create another function and this one is going to be it's going to be called get latest file and this is going to take two arguments it will be folder and there will be a folder destination right folder being the sharepoint folder folder destination oops let me fix this folder destination being where you want to save it to okay so let's um what we're going to end up doing here we're going to call our sharepoint class and then we're going to call our new function that we created which is the download latest file this is going to end up taking one argument which would be folder right again sharepoint folder that's what this is now it is returning back and we take a look at it it is returning back uh two values the file name and the contents so what we're gonna do here is we're going to call this file name because that's the first um value and then contents that's we're going to get back so based on the value that we get back now we could call the save file function and again we're going to pass in file name we're going to pass in the file object which is our content and then we're going to pass in the folder destination again which is this argument here this is to tell it where we want to save it to pretty much and that is it guys next let's just do a um add some logic where once when it once this file runs we wanted to execute this code here in line 20 which would be get latest file and then all ultimate we're passing in the two arguments that we're going to provide which is folder name and then folder destination right which is these two arguments up here we're gonna pass in down here um so now we go ahead and if i run this which is going to be which is again we're gonna execute this file download latest file we're passing our first argument which is uh data 2022 sales so let's take a look at that if i look at the data 2022 sales folder my newest file in here is this guy right here this lone file so that's the file that should be downloaded then i'm going to save it in this folder now let's take a look at the folder right now it's empty so when i run it it should save into that folder so let's run it let's see what we get all right so boom there you go it downloaded the latest file which is awesome because that's what we wanted man that's exactly what we wanted we want to be able to access a folder and download the latest file so this this did exactly what we expected and it's all wrapped up in this function that we created called download latest file so this this sample these files that i have here this code this is strictly for a sample it's an example on how to use it and you can run it right out the box and start using it you can use it in your projects you don't have to use a code that i have here i know i got a message from one of the viewers like yeah your examples are um over not over complicated but um they're they're too much and he's right i mean i could have kept it way more simpler to ultimately just to show you an example but i try to build something that was a little bit more than that so for the people that wanted like just to take a file as is and literally just run it like the way i did right past arguments run it and you're good to go try to keep it as simple as possible for those people for the people that have different ways to use it perfectly understand all you need to do just call this class and use it whatever way that fits best for you i am going to create a folder and i'm going to call this uh um examples and then of course i'm going to just move all of these files in here into this new folder so um it'll make it a little bit cleaner once i i'm going to push this code over to my github account so you can find the source code there and you'll see a folder with all the examples in here again guys this was was a request from a viewer they asked hey can you help me out how can we download the latest file in a um sharepoint folder so hopefully this helps you out this kind of solved that issue again guys if you need any help if you have any questions you could email me at contact iamblue.net or just leave a comment and when i read my comments i'll add your request to the list of uh requests that i've been getting um can't guarantee it can't promise i will get to all requests because i have been getting more and more but nevertheless guys i will be making videos of your requests again appreciate it hit that like button give me a follow and peace
Info
Channel: I am Lu
Views: 2,240
Rating: undefined out of 5
Keywords:
Id: pNkg3jmiEe4
Channel Id: undefined
Length: 21min 32sec (1292 seconds)
Published: Sat Aug 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.