Python Error Help, Date Formatting

2161
6
12-30-2019 08:59 AM
SamLee1
New Contributor
#!/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.

Tags (2)
0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Your current code is converting a datetime directly to a string, which includes excess information.  Use datetime string formatting (8.1. datetime — Basic date and time types — Python 2.7.17 documentation ) to get what you want:

>>> from datetime import datetime
>>> date1 = datetime.now()
>>> str(date1)
'2019-12-30 12:01:09.852842'
>>>
>>> date1.strftime('%d-%m-%Y')
'30-12-2019'
>>>
0 Kudos
SamLee1
New Contributor

Update: Get this error with those two additions from your hint.

date1 = time.ctime(os.path.getmtime(path))
date1 = str(date1)
date1= date1.strftime('%d-%m-%y')
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The error indicates you somewhere converted a datetime object to a string object, and string objects don't have such method.  Look upstream in your code and make sure you are not converting a datetime object to string.

SamLee1
New Contributor

I was able to get pass the syntax error but the expected result didn't show.

Expected Result:

Old file is supposed to be renamed Grid_Support_....._2019-12-30

New File is supposed to be named Grid_Support_.....with no date on it

New file is supposed to be inside Folder directory but for some reason it moved to Archived Directory

Old file is moved to the archived directory

I have attached a copy of the excel file in case you wanted to test it on your computer.

The date on it is 12-30-2019 so you can actually use it. If it was 12-31-2019 then the results that

you wanted to see won't show.

Code:

#!/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).replace(":"," "))+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()   
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Actual Result:

0 Kudos
Arne_Gelfert
Occasional Contributor III

This is pretty confusing to read... why don't you stick with the pattern:

filename = "myfile"
directoryname = r"\\mydirectory\mysubdirectory"
fullpath = os.path.join(directoryname,filename)‍‍‍‍‍‍‍‍‍‍‍‍‍‍

That's not only easier to read but avoids all this concatenating of filenames and folder names and "\\". I'm not surprised something is ending up in the wrong folder.

SamLee1
New Contributor

Update:

How to I fix the fille name of the file in the archived folder to show file name + modified date?

Modified date: 2019-12-30

Disregard the first file. I fixed the code so both files don't show in archived folder. I renamed path to path1 since

path is already used previously, so I made a new variable called path1.

0 Kudos