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.
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 the below code which downloads a sheet to a folder on my computer. How do I have it download the excel sheet to a newly created folder with the current day's datestamp? So for example, I want the file to download to a folder called:
C:/Users/E29853/OneDrive/Smartsheets/Templates/20220610/
for any files downloaded on June 10, 2022.
This is the code I have:
import os, smartsheet
token=os.environ['SMARTSHEET_ACCESS_TOKEN']
smartsheet_client = smartsheet.Smartsheet(token)
smartsheet_client.errors_as_exceptions(True)
smartsheet_client.Sheets.get_sheet_as_excel(
8729488427892475,
'C:/Users/E29853/OneDrive/Smartsheets/Templates',
'Region.xlsx'
)
In order to augment your existing code to achieve your stated objective, you need to know how to achieve the following two things with Python:
how to get the current date (string) in yyyymmdd format
how to create a new directory if it doesn't already exist
I'm fairly new to Python myself, but was able to figure this out thanks to Google. In case it's helpful for you in the future, here was my process for figuring this out.
Step 1: Determine how to get the current date (yyyymmdd) in Python
Google search for python get current date yyyymmdd
The top search result was a Stack Overflow answer with > 1000 upvotes (which indicates a broadly approved answer that should be reliable).
Note that the date format was slightly different in this question/answer (yyyy-mm-dd) -- I omitted the hyphens in my code, to get the desired format yyyymmdd.
Now that I know how to get the date string in the desired format, I'll be able to concatenate it with the string that represents my base path, to get my target path:
# specify path
path = 'c:/users/kbrandl/desktop/' + current_date
Step 2: Determine how to create a directory (if it doesn't already exist) in Python
Google search for python create folder if not exists
Once again, the top search result provided the sample code I was looking for.
With this info, I now know how to create my target directory (folder) if it doesn't yet exist:
# create directory if it doesn't exist
if not os.path.exists(path):
os.mkdir(path)
Putting this all together now...the following code achieves your stated objective.
import os, smartsheet
from datetime import datetime
sheetId = 3932034054809476
# get current date in yyyymmdd format
current_date = datetime.today().strftime('%Y%m%d')
# specify path
path = 'c:/users/kbrandl/desktop/' + current_date
# create directory if it doesn't exist
if not os.path.exists(path):
os.mkdir(path)
# download file to specified path
smartsheet_client.Sheets.get_sheet_as_excel(
sheetId,
path,
'MyFileName.xlsx'
)
I am looking to stack certain GeoTiffs according to todays, date, since this will run daily, and hence proper layers need to be stacked.
#get current date
from datetime import datetime
datetime.today().strftime('%Y%m%d')
from osgeo import gdal
outvrt = '/vsimem/stacked.vrt' #/vsimem is special in-memory virtual "directory"
outtif = 'C:\Program Files\GeoServer\data_dir\data\RSS_WH/stacked.tif'
import glob
tifs = glob.glob('C:\Program Files\GeoServer\data_dir\data\RSS_WH/*.tif')
outds = gdal.BuildVRT(outvrt, tifs, separate=True)
outds = gdal.Translate(outtif, outds)
As you can see, currently, the end of it is only *.tif, meaning all the tif files in the folder would be stacked together. However, I need to insert the current date into the pathway so that everything, that contains for instance 12082021 and stuff before and after it should be included (because layers differ based on prefix and suffix, and the date is the only reliable constant.
This is a straightforward modification of the code you have.
First, make sure you're assigning the current date to a variable, rather than just printing it out:
today = datetime.today().strftime('%Y%m%d')
Then include this in the search string in your glob command (making sure to put an asterisk on either side, if you want matches wherever the date is in the filename):
tifs = glob.glob('C:\Program Files\GeoServer\data_dir\data\RSS_WH/*'+today+'*.tif')
or, equivalently:
tifs = glob.glob('C:\Program Files\GeoServer\data_dir\data\RSS_WH/*{}*.tif'.format(today))
I have files being put into a folder. Each day I would like to take those files and move them into a folder with that days date as the folder name. I've been able to create the folder using the current date
import shutil, os
import time
date = time.strftime("%Y%m%d")
parent_dir = "C:\dfolder"
path = os.path.join(parent_dir, date)
os.mkdir(path)
This was successful creating the folder. My problem is the part of the code that will find that newly created folder each day and move the files into it. I have been able to use shutil.move to move the files into the folder but I have to specify the name of the destination. Is there a way to automate this each day? Maybe by having it put the files for that day into the most recently created folder or something of the sort?
How about this: It makes a new folder for tomorrow and renames today's folder:
import shutil, os
import time
date = time.strftime("%Y%m%d")
parent_dir = "C:\dfolder"
path = os.path.join(parent_dir, date)
os.rename("New folder", path) // Renames today's folder to it's proper name
os.mkdir("New folder") // Makes the new folder for tomorrow
This is better than checking the dates of folders being made. It just does it for you for even more automation.
Using the os.rename is working, however it is also moving my file.
import shutil, os
import time
date = time.strftime("%Y%m%d")
parent_dir = "C:\wfolder"
os.rename (parent_dir , date)
Any thoughts?
I had a similar problem a while back. If this folder is not used for anything else the best solution is to keep a list of current files in the folder. The folder can be checked with os.listdir(). Then you can compare the two list and take the difference which leaves you with the most newly add file. The result of os.listdir() becomes your new baseline. I will try to find the exact code I used and place it here.
Code:
old_list= os.listdir(your_path)
## Your function for creating folder here ##
new_list= os.listdir(your_path)
dl = []
for item in new_list:
if item not in old_list:
dl.append(item)
dl # is the list of all new files in the directory #
How do I make the filepath dynamic. I have to pick info from 7 spreadsheets and I have to just daily change the dates in these file paths. How can i declare the date and call the date in a string.
date = 20200607
filepath = HKTR Report\06. June**20200605**\Summary006T-CTRD2603-FX-20200605.csv"
The two dates in the file path have to be replaced with date declared making the filepath dynamic.
Kindly assist.
Welcome to Stack Overflow!
The path you have listed is peculiar in that it contains star characters which are not usually used in pathnames.
That said, try using this code:
from datetime import date
d = date.today()
date_str = d.strftime('%Y%m%d')
print(date_str)
filepath = f'HKTR Report\\06. June**{date_str}**\\Summary006T-CTRD2603-FX-{date_str}.csv'
print(filepath)
which generates this output:
20200608
HKTR Report\06. June**20200608**\Summary006T-CTRD2603-FX-20200608.csv