samlee916

Python Error Help, Date Formatting

Discussion created by samlee916 on Dec 30, 2019
Latest reply on Dec 31, 2019 by samlee916
#!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
import time

def getFile():
    url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
    content = requests.get(url)
    date = datetime.date(datetime.now())
    directory = "C:\\Users\\slee5\\Desktop\\Folder"
    archivedDirectory = "C:\\Users\\slee5\\Desktop\\Folder\\ArchivedFolder"
    #date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))
    fileName = "Grid_Support_Inverter_List_Full_Data"
    extension = ".csv"
    path = fileName + extension
    print ("Files will be saved in the following directory: "+directory+"\n")
    print ("Old Files with the same name will be archived in the following directory: "+archivedDirectory+"\n")
    print ("NOTICE: File will be converted to .csv file...")
    if os.path.isdir(directory):
        #Check if the Archived Folder Exists, if it does not create it
        if(os.path.isdir(archivedDirectory)):
            pass
        else:
            os.mkdir(archivedDirectory)
        print ("SUCCESS: Directory was found.")
        counter = 0
        if (os.path.exists(directory+"\\"+fileName+extension)):
            counter += 1
            while (os.path.exists(archivedDirectory+"\\"+fileName+"_%s_%s"%(str(date), str(counter))+extension)):
                counter += 1
        if counter > 0:
            print ("File: "+fileName+" already exists in directory,moving old file to Archived Folder. ")
            shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension)
            path = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
            date1 = time.ctime(os.path.getmtime(path))
            os.rename(path,archivedDirectory+"\\"+fileName+"_%s"%(str(date1))+extension)
        with open(os.path.join(directory, path), 'wb') as f:
            f.write(content.content)
        deleteRows(directory, path)
        print("Done.")
    else:
        print("ERROR: Directory is not found.")
        sys.exit()
def deleteRows(directory, path):
    newFile = pd.read_excel(os.path.join(directory,path))
    newFile = newFile.drop(range(14), axis = 0)
    newFile.to_csv(os.path.join(directory, path), encoding='utf8', header=False, index=False)
def main():
    getFile()

if __name__ == "__main__":
    main()  

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.

Outcomes