Hello,
When I run my python script, it downloads a file from a website to a directory and it moves an old file from that directory to an archived folder and renames it with filename+date moved/date when script runs.
I am trying to fix it to make it show filename+modified/created date instead of filename+date moved/date when script runs.
This file is in the archived folder by the way.
Attached is the python code.
I'd be happy to look at your script, but downloading a zip file makes our security team crazy. If you could just use the syntax highlighter and paste it there, that would be great....
#!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
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\slee2\Folder"
archivedDirectory = "C:\Users\slee2\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)
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()
Sorry I didn't know how to paste code into these and now I do, thanks.
Take a look here: How to get file creation & modification date/times in Python? - Stack Overflow (I just googled 'extract date modified from windows file in python' to find it and others...)
Hello Joe,
Yes I used the exact same link as you and still didn't get anything.
I stored it into a variable and it didn't work.
Well, shoot! Thought that would get you going. There are a couple guys out there lurking that probably have a solution for you...
In your code at line 9, you are getting the current date/time and using it in lines 32 and 36. I'm not sure your code inside the loop starting at line 22 is doing what you expect. But for now, just focusing on the date issue, I would suggest trying something like this to replace lines 29-36:
if (os.path.exists(directory+"\\"+fileName+extension)):
modDate = datetime.fromtimestamp(os.path.getmtime(directory+"\\"+fileName+extension))
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+str(modDate)[:10]+extension)
Back to the loop, at line 32, do you want to check if multiple files exist that have the current date in the file name?
Hope this helps.
Hello Randy,
I get an error from doing that. Been stuck on this issue for a long time, used every single online resource out there and still couldn't fix the issue.
The error only occurs if I already have a file in the folder directory.
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)
path1 = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
date1 = datetime.fromtimestamp(os.path.getmtime(path1))
date1 = str(date1)
date1 = date1.strftime('%y-%m-%d')
os.rename(path1,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()
Here is some background information for the script:
Try replacing lines 39-42 in your latest code with (maintaining the indentation):
date1 = datetime.fromtimestamp(os.path.getmtime(path1))
# date1 = str(date1) # delete or comment out - this line is not needed and is causing the error
date1 = date1.strftime('_%y-%m-%d') # this converts datetime object to a formatted string - add and underscore to format
os.rename(path1,archivedDirectory+"\\"+fileName+date1+extension) # previous line formats date1 to text
The os.path.getmtime returns a timestamp which is converted by datetime.fromtimestamp into a datetime object ( year, month, day, hours, minutes, seconds). Since date1 is now a datetime object, strftime can be used to format it into a string "_yy-mm-dd". Once it is a string, it can be added into the file's name sequence: fileName+date1+extension.
In my previous code snippet, I was renaming the file as it was being moved. In this snippet the file is renamed after it was moved. Either way should work for you.
Hello Randy,
Thanks so much for the help, been stuck on this for a week.
One small error, how can I change 19-12-30 to 2019-12-30?
Code In Case:
#!/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)
path1 = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
date1 = datetime.fromtimestamp(os.path.getmtime(path1))
#date1 = str(date1) # delete or comment out - this line is not needed and is causing the error
date1 = date1.strftime('_%y-%m-%d') # this converts datetime object to a formatted string - add and underscore to format
os.rename(path1,archivedDirectory+"\\"+fileName+date1+extension) # previous line formats date1 to text
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()