I am trying to delete old folders and I am asking does anyone know how to set up a variable that allows me to check the variable 'todaystr' which is today's date and minus 7 days of this string and store it another variable. I am wanting to automatically delete old files after a week. Below shows the variable 'todaystr' being set up.
todaystr = datetime.date.today().isoformat()
I would like to create a variable 'oldfile' that stores the current date minus 7 days so I can delete the file with this date. Thanks for any help.
import datetime
import os
import shutil
threshold = datetime.datetime.now() + datetime.timedelta(days=-7)
file_time = datetime.datetime.fromtimestamp(os.path.getmtime('/folder_name'))
if file_time < threshold:
shutil.rmtree('/folder_name')
I relation to the above answer it works very well, the code I used was different in the end. I create the name of the folder with the current date, so when the nightly build runs it will only delete the folder named from 7 days ago. The code is as follows:
import datetime
import os
import calendar
today = datetime.date.today()
todaystr = datetime.date.today().isoformat()
minus_seven = today.replace(day=today.day-7).isoformat()
if os.path.exists(minus_seven):
os.system("sudo rm -rf "+minus_seven)
print 'Sandboxes from 7 days ago removed'
I used linux the delete the folder as I have some linux incorporated into my code and it runs good like this.
Related
I am looking to create a python script which can take a date to move file from one s3 folder to another s3 folder. Now while moving it uses created date to create folder in target i.e. stage/2023/01/12 and copy the file to this new folder.
Thanks
Param
I have used boto3 but not sure how to achieve that
To get the modification or creation date of each file, look here. It explains how to get the modification or creation date (returned as POSIX timestamp, i.e. seconds from the Unix epoch, January 1 1970).
You'll likely want to make the POSIX timestamp easier to work with by using the python datetime module; you'll begin by converting to a datetime object with date.fromtimestamp(your_posix_timestamp_here)
To programmatically create folders for year, month, and day, and copy the file to that folder: First pull the year, month, and day out of the datetime object, then do something like this:
#!/usr/bin/env python3
import os
from pathlib import Path
Path('2023/01/12/').mkdir(parents=True, exist_ok=True) # make nested folders for year, month, day
shutil.move("path/to/current/file.foo", "2023/01/12/file.foo") # move the file
Hope that helps!
I have a number of csv files (6) in a Linux folder that I need to rename and relocate to a new folder on the same server.
<entity_name>_yyyymmdd_hhmmss.csv - bearing in mind the <entity_name> is a string that varies from file to file.
I need to be able to keep the original <entity_name> but replace the yyyymmdd_hhmmss with to day's date in the format yyyymmdd, so what we end up with is <entity_name>_yyyymmdd.csv
if this could be done using Python thanks.
Being new to Python the Internet was awash with ideas, some were close, but none seemed to help me achieve what I am after.
I have successfully manged to loop through the folder I need and read each file name, but am stuck renaming the files.
It's just straight-line coding. For each file, extract the base, add the date and extension, and rename.
import os
import glob
import datetime
today = datetime.date.today().strftime('%Y%m%d')
newpath = "wherever/we/go/"
for name in glob.glob('*.csv'):
base = name.split('_',1)[0]
newname = f'{base}_{today}.csv'
os.rename( name, newpath+newname )
I have say a/b/c/date/Xyz.txt how can I pick up the path dynamically, date is changing randomly. Other directory fields remains the same like a/b/c/dateIsChanging/Xyz.txt
Someone is adding file random date directory, how can I get the path, each time when it is added
a/b/c/dateIsChanging/Xyz.txt here in directory under random chosen by the user, I want to pick it up whatever date comes in
Date in YYYYMMDD formate
This is an example of what one would do:
import os
import datetime as dt
# create a date for folder name
date_part = dt.datetime.today().strftime('%Y-%m-%d')
# generate the folder path
the_path = os.path.join('C:\\','test',str(date_part),'file_name.txt')
print(the_path)
the output:
C:\test\2022-03-24\file_name.txt
Obviously, would need to adjust for your particular operating system and date format.
I'm trying to solve a problem at work, I am not a developer but work in general IT operations and am trying to learn a bit here and there, so I may be way off right now with what I'm trying to do here. I've just been utilizing online resources, here and a little bit from the book Automate the Boring Stuff with Python. Here is my objective:
I have two files that are automatically placed in a folder on my computer every morning at the same time using a post processor, and I need to add yesterday's date to the end of the file names before I upload them to an FTP server which I have do each morning around the same time. I am trying to write a Python script that I can somehow schedule to run each morning right after the files are placed in the folder, which will append yesterday's date in MMDDYYYY format. For example, if the files are called "holdings.CSV" and "transactions.CSV" when they are placed in the folder, I need to rename them to "holdings01112022.CSV" and "transactions01112022.CSV". I only want to rename the new files in the folder, the files from previous days with the dates already appended will remain in the folder. Again, I'm a total beginner, so my code may not make sense and there may be superfluous or redundant lines, I'd love corrections... Am I going down the right path here, am I off altogether? Any suggestions?
import os, re
from datetime import date
from datetime import timedelta
directory = 'C:\Users\me\main folder\subfolder'
filePattern = re.compile('%s.CSV', re.VERBOSE)
for originalName in os.listdir('.'):
mo = filePattern.search(originalName)
if mo == None:
continue
today = date.today()
yesterday = today - timedelta(days = 1), '%M%D%Y'
for originalName in directory:
newName = originalName + yesterday
os.rename(os.path.join(directory, originalName), os.path.join(directory, newName))
Any help is appreciated. Thanks.
Here's a short example on how to code your algorithm.
import pathlib
from datetime import date, timedelta
if __name__ == '__main__':
directory = pathlib.Path('/Users/cesarv/Downloads/tmp')
yesterday = date.today() - timedelta(days=1)
for file in directory.glob('*[!0123456789].csv'):
new_path = file.with_stem(file.stem + yesterday.strftime('%m-%d-%Y'))
if not new_path.exists():
print(f'Renaming {file.name} to {new_path.name}')
file.rename(new_path)
else:
print(f'File {new_path.name} already exists.')
By using pathlib, you'll simplify the handling of filenames and paths and, if you run this program on Linux or macOS, it will work just the same.
Note that:
We're limiting the list of files to process with glob(), where the pattern *[!0123456789].csv means "all filenames that do not end with a digit before the suffix (the [!0123456789] represents one character, before the suffix, that should not - ! - equal any of the characters in the brackets)." This allows you to process only files that do not contain a date in their names.
The elements given by the for cycle, referenced with the file variable, are objects of class Path, which give us methods and properties we can work with, such as with_stem() and stem.
We create a new Path object, new_path, which will have its stem (the file's name part without the suffix .csv) renamed to the original file's name (file.name) plus yesterday in the format you require by using the method with_stem().
Since this new Path object contains the same path of the original file, we can use it to rename the original file.
As you can see, we can also check that a file with the new name does not exist before any renaming by using the exists() method.
You can also review the documentation on pathlib.
If you have any doubts, ask away!
Are you running to any issue? If all you want is to get the csv file you can simplify your code by using one if statement instead of using regex. You also need to convert your date to string before adding it to your file name. (i.e: newName = originalName[:-3] +str(yesterday)).
If this code didnt solve your problem please mention the error that you are receiving to make it easier for others to help you.
Here is my suggestion to eliminate possible errors without knowing what type of error you are getting.
import os, re
from datetime import date
from datetime import timedelta
directory = 'C:/Users/me/main folder/subfolder'
for originalName in os.listdir(directory):
print(f'original name is = {originalName}')
if originalName[-3:].lower() =='csv':
today = date.today()
yesterday = today - timedelta(days = 1)
yesterday_file_name = originalName[:-4]+yesterday.strftime("%m%d%Y")+'.csv'
os.rename(os.path.join(directory, originalName), os.path.join(directory, yesterday_file_name))
#print('formatted file names are')
#print(yesterday_file_name)
#here is the sample output
#original name is = holdings.csv
#formatted file names are
#holdings01112022.csv
#original name is = transactions.CSV
#formatted file names are
#transactions01112022.csv
I used the following function to created dirctory based on today date ,
#!/usr/bin/python
import time, datetime, os
today = datetime.date.today()
todaystr = today.isoformat()
os.mkdir(todaystr)
so the out put will be
/2015-12-22/
what i'm looking to is adjust the structure which is create dirctories structure based on day date as following
/2015/12/22
/2015/12/23
etc
when ever i run the function it will check the date and make sure the folder is exist other wise will create it ..
any tips to follow here ?
Consider using strftime instead. Which you can use to defined a format to your liking. You will also need to use os.makedirs as described by #Valijon below.
os.makedirs(time.strftime("/%Y/%m/%d"), exist_ok=True)
You can also append a given time to create a time-stamp in the past or in the future.
time.strftime("/%Y/%m/%d", time.gmtime(time.time()-3600)) # -1 hour
Also note that your path is a bit dangerous, unless you want to create folders directly under the root partition.
Note that makedirs will raise an exception by default if the directory already exists, you can specify exist_ok=True to avoid this, read more about it in the docs for os.makedirs.
Since Python 3.4, the module pathlib was Introduced which offers some directory and file creation features.
import time
import pathlib
pathlib.Path(time.strftime("/%Y/%m/%d")).mkdir(parents=True, exist_ok=True)
Just change os.mkdir to os.makedirs
os.makedirs(today.strftime("%Y/%m/%d"))