<SPAN class="comment token">#!/usr/bin/env python</SPAN>
<SPAN class="keyword token">import</SPAN> requests
<SPAN class="keyword token">import</SPAN> os<SPAN class="punctuation token">.</SPAN>path
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> shutil
<SPAN class="keyword token">import</SPAN> pandas <SPAN class="keyword token">as</SPAN> pd
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">import</SPAN> time
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getFile</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
url <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"</SPAN>
content <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN>
date <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">(</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
directory <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\Users\\slee5\\Desktop\\Folder"</SPAN>
archivedDirectory <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\Users\\slee5\\Desktop\\Folder\\ArchivedFolder"</SPAN>
<SPAN class="comment token">#date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))</SPAN>
fileName <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Grid_Support_Inverter_List_Full_Data"</SPAN>
extension <SPAN class="operator token">=</SPAN> <SPAN class="string token">".csv"</SPAN>
path <SPAN class="operator token">=</SPAN> fileName <SPAN class="operator token">+</SPAN> extension
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Files will be saved in the following directory: "</SPAN><SPAN class="operator token">+</SPAN>directory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Old Files with the same name will be archived in the following directory: "</SPAN><SPAN class="operator token">+</SPAN>archivedDirectory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"NOTICE: File will be converted to .csv file..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>isdir<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Check if the Archived Folder Exists, if it does not create it</SPAN>
<SPAN class="keyword token">if</SPAN><SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>isdir<SPAN class="punctuation token">(</SPAN>archivedDirectory<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">pass</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
os<SPAN class="punctuation token">.</SPAN>mkdir<SPAN class="punctuation token">(</SPAN>archivedDirectory<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"SUCCESS: Directory was found."</SPAN><SPAN class="punctuation token">)</SPAN>
counter <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN>directory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN>extension<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
counter <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">while</SPAN> <SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN>archivedDirectory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN><SPAN class="string token">"_%s_%s"</SPAN><SPAN class="operator token">%</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>date<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>counter<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>extension<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
counter <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">if</SPAN> counter <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"File: "</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN><SPAN class="string token">" already exists in directory,moving old file to Archived Folder. "</SPAN><SPAN class="punctuation token">)</SPAN>
shutil<SPAN class="punctuation token">.</SPAN>move<SPAN class="punctuation token">(</SPAN>directory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN>extension<SPAN class="punctuation token">,</SPAN> archivedDirectory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN><SPAN class="string token">"_%s"</SPAN><SPAN class="operator token">%</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>date<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>extension<SPAN class="punctuation token">)</SPAN>
path <SPAN class="operator token">=</SPAN> archivedDirectory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN><SPAN class="string token">"_%s"</SPAN><SPAN class="operator token">%</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>date<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>extension
date1 <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>ctime<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>getmtime<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
os<SPAN class="punctuation token">.</SPAN>rename<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN>archivedDirectory<SPAN class="operator token">+</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="operator token">+</SPAN>fileName<SPAN class="operator token">+</SPAN><SPAN class="string token">"_%s"</SPAN><SPAN class="operator token">%</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>date1<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>extension<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">,</SPAN> path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'wb'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> f<SPAN class="punctuation token">:</SPAN>
f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>content<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">)</SPAN>
deleteRows<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">,</SPAN> path<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Done."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ERROR: Directory is not found."</SPAN><SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">deleteRows</SPAN><SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">,</SPAN> path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
newFile <SPAN class="operator token">=</SPAN> pd<SPAN class="punctuation token">.</SPAN>read_excel<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">,</SPAN>path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
newFile <SPAN class="operator token">=</SPAN> newFile<SPAN class="punctuation token">.</SPAN>drop<SPAN class="punctuation token">(</SPAN>range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">14</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> axis <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
newFile<SPAN class="punctuation token">.</SPAN>to_csv<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">,</SPAN> path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> encoding<SPAN class="operator token">=</SPAN><SPAN class="string token">'utf8'</SPAN><SPAN class="punctuation token">,</SPAN> header<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN> index<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
getFile<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Need help with getting the correct date modified format in dd-mm-yyyy. I tried all the available online resources.
Error Image:

Background Information:
The script moves the old file into an archived directory and renames it with the file name + modified date. The modified date is what I am trying to get and its correct format.