Renaming csv file with a variable name and moving to destination folder - python

I have a csv file in downloads folder with a name export.csv.
I need to rename the file in the format "REMSIMPORT + today's date" and move to destination folder.
For e.g. today's date is 2022-01-24. So export.csv will be renamed as REMSIMPORT20220124.
Tomorrow it will be REMSIMPORT20220125.
So it will change every time as per current date
This is how far I have reached.
from datetime import date
src_path = r'C:/Users/abc/Downloads/'
dest_path = r"C:\Users\abc\Desktop\Weekly"
today_date = date.today() # Extracted today's date
today_date = str(today_date).replace("-","")
I am confused on how to rename file with a variable newname (e.g. REMSIMPORT20220124) and moving to destination folder.

Use the rename method from os module.
import os
src_path = r'C:/Users/abc/Downloads/' + 'REMSIMPORT'
dest_path = r'C:/Users/abc/Desktop/Weekly/' + 'REMSIMPORT' + today_date
os.rename(src_path, dest_path)

You may use schedule for your purpose.
$ pip install schedule
Here is some examples of schedule from the page :
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
On your purpose, you can try something like this :
from datetime import date
import pandas as pd
import schedule
import time
def save_csv():
today_date = str(date.today()).replace("-","")
src_path = r'C:/Users/abc/Downloads/'
dest_path = r'C:\Users\abc\Desktop\Weekly'
df = pd.read_csv(src_path + 'export.csv')
df.to_csv(dest_path + 'REMSIMPORT' + today_date + '.csv')
schedule.every().day.at("10:30").do(save_csv)
while True:
schedule.run_pending()
time.sleep(1)

Shutil copyfile should be good!
from datetime import date
from shutil import copyfile
src_path = r'C:/Users/user/adifferentdir'
dest_path = r"C:/Users/user/dir"
today_date = date.today() # Extracted today's date
today_date = str(today_date).replace("-","")
copyfile(src_path+"/export.csv",dest_path+"/REMSIMPORT"+today_date+".csv")#copying file with new name```

I think using the shutil.move() utility function would be the best option because not only can it move files, it can rename them at the same time. I've also found that using the pathlib module makes file and directory path manipulations not only extremely easy but at very intuitive (and allow doing so in an OS portable way).
Anyway, here's an example of putting them to use to accomplish what you want to do.
from datetime import date
from pathlib import Path
from shutil import move
filename = 'export.csv'
#src_dir = Path('C:/Users/abc/Downloads/')
#dst_dir = Path('C:/Users/abc/Desktop/Weekly')
# Directories for testing.
src_dir = Path('./').resolve()
dst_dir = Path('./Weekly').resolve()
prefix = 'REMSIMPORT'
today_date = date.today() # Extracted today's date
today_date = str(today_date).replace('-', '')
newname = prefix + today_date
if not dst_dir.exists():
dst_dir.mkdir() # Create destination directory since it doesn't exist.
src_path = src_dir / filename
dst_path = dst_dir / (newname + src_path.suffix)
move(src_path, dst_path)
print('fini')

Related

Why does this script move all files instead of just the newest files?

import os
import datetime
import shutil
source = 'C:/Users/user/Desktop/Files to move/'
destination = 'C:/Users/user/Desktop/Delete Logs/'
today = datetime.datetime.today() # Get current time
# Create log file with datestamp
file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt', 'a')
# Move files
allfiles = os.listdir(source)
for f in allfiles:
# Check last modified time
t = os.stat(os.path.join(f, source))[8]
filetime = datetime.datetime.fromtimestamp(t) - today
# Is file less than a day old? If yes, move.
if filetime.days <= 1:
print(os.path.join(f, source), filetime.days)
file.write(os.path.join(f, source) + ' created ' + str(-1 * filetime.days)+' day(s) ago has moved\n')
shutil.move(source + f, destination + f)
Like the title says, I wrote this trying to move files less than a day old to a new location on a different disk but it moves all files in the location instead of the newest files. What am I doing wrong?
Your code works fine, you just need to add '-' sign before the last condition, because the output of filetime.days is -1, your condition will be :
if -filetime.days <= 1:
And you have inversed between source and filename just after the for loop
import os
import datetime
import shutil
source = 'f2/'
destination = 'f1/'
today = datetime.datetime.today() # Get current time
# Create log file with datestamp
file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt', 'a')
# Move files
allfiles = os.listdir(source)
for f in allfiles:
# Check last modified time
t = os.stat(os.path.join(source, f))[8]
filetime = datetime.datetime.fromtimestamp(t) - today
print (filetime.days, type(filetime.days), t, f)
# Is file less than a day old? If yes, move.
if -filetime.days <= 1: # ==============> Here
print(os.path.join(f, source), filetime.days)
file.write(os.path.join(f, source) + ' created ' + str(-1 * filetime.days)+' day(s) ago has moved\n')
shutil.move(source + f, destination + f)

PYTHON: How should I change my code to reorganize files by day rather than by month?

I've got a bit of code that renames and reorganizes NetCDF files into monthly folders. I would like to change this code so that it gives me daily folders rather than monthly folders.
import os
from datetime import datetime
destination_directory = input("Now please provide the destination directory in the same format:")
wd = os.chdir(destination_directory)
for f in os.listdir():
file_name, file_ext = os.path.splitext(f)
f_1, f_2, date = file_name.split('.')
dt = datetime.strptime(date,"%Y%m%d%H%M")
newname = "{}{}{}{}.{}".format(str(dt.year),str(dt.month),str(dt.day),str(dt.time().hour)+str(dt.time().minute),'nc')
dst = os.path.join(str(dt.year),str(dt.month),newname)
if os.path.isdir(os.path.join(str(dt.year),str(dt.month))) == False:
os.makedirs(os.path.join(str(dt.year),str(dt.month)))
os.rename(f, dst)
print(dst, newname, 'created')
All you have to do is to add day to your paths. Like this:
dst = os.path.join(str(dt.year),str(dt.month),str(dt.day),newname)
if os.path.isdir(os.path.join(str(dt.year),str(dt.month),str(dt.day))) == False:
os.makedirs(os.path.join(str(dt.year),str(dt.month),str(dt.day)))

Python: How to delete the folders called trash that are older than 3 months

I'm looking to create a script that will look through specific directories and its subdirectories, and if it contains a folder called trash, and it is older than 3 months it will delete that trash folder and its contents.
The code i have already tried is shown below. It contains a couple of options of how to get the time of 3 months. I have also used os.walk to try to traverse the directories, but i have not managed to target just the trash folders and then remove them.
import os, sys, time
from datetime import datetime
from datetime import timedelta
from subprocess import call
import path
now = time.time()
cutoff = now - (30)#(91 * 86400)
three_months = datetime.today() - timedelta(0,0,0,0,0,1)
path = '/users/shoot_station_5/documents/folderfordeletetest/'
for root, d_names, f_names in os.walk(path):
print (root, d_names, f_names)
for _dir in d_names:
time_thing = os.path.getmtime(os.path.join (root, _dir))
time_thing1 = datetime.fromtimestamp(time_thing)
if time_thing1 <= three_months and _dir == 'trash':
# fname = os.path.join(d_names, _dir)
# if time.ctime(os.path.getmtime(os.path.join (root, _dir))) < cutoff:
print ('good')
Below code delete a directory called trash that are older than 3 months and all its contents.
import os
import datetime
import shutil
def Delete_Folder(filesPath,No_of_Days):
for root, d_names, f_names in os.walk(filesPath):
for _dir in d_names:
if str(_dir).lower() == "trash":
DName = os.path.join (root, _dir)
today = datetime.datetime.today()
modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(DName))
duration = today - modified_date
if (duration.days > No_of_Days):
shutil.rmtree(DName)
# Call Function
NoDays = 90
filesPath = r"C:\Sample"
Delete_Folder(filesPath,NoDays)

How to use getmtime for multiple files

My current python script:
import ftplib
import hashlib
import httplib
import pytz
from datetime import datetime
import urllib
from pytz import timezone
import os.path, time
import glob
def ftphttp():
ts = os.path.getmtime('Desktop/images/frame00.png')
dt = datetime.fromtimestamp(ts, pytz.utc)
timeZone= timezone('Asia/Singapore')
#converting the timestamp in ISOdatetime format
localtime = dt.astimezone(timeZone).isoformat()
cam = "002"
lscam = localtime + cam
ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
ftp.cwd('/var/www/html/image')
m=hashlib.md5()
m.update(lscam)
dd=m.hexdigest()
for image in glob.glob(os.path.join('Desktop/images/frame**.png')):
with open(image, 'rb') as file:
ftp.storbinary('STOR '+dd+ '.png', file)
x = httplib.HTTPConnection('localhost', 8086)
x.connect()
f = {'ts' : localtime}
x.request('GET','/camera/store?cam='+cam+'&'+urllib.urlencode(f)+'&fn='+dd)
y = x.getresponse()
z=y.read()
x.close()
ftp.quit()
As right now this line of code is only get one file timestamp:
ts = os.path.getmtime('Desktop/images/frame00.png').
But what if i send multiple file from a folder and get all the files timestamp. Is it possible to do it? I using ftplib to send multiple from a folder to another folder.
You could just walk the directory and getmtime for all the files in that directory if the directory in question is Desktop/images
You can replace
ts = os.path.getmtime('Desktop/images/frame00.png')
dt = datetime.fromtimestamp(ts, pytz.utc)
With something like:
dirToCheck = 'Desktop/images'
for root, _, file in os.walk(dirToCheck):
fileToCheck = os.path.join(root, file)
ts = os.path.getmtime(fileToCheck)
dt = datetime.fromtimestamp(ts, pytz.utc)
print ts, dt
With this approach you will need to give the absolute path to the images directory and also maybe store the timestampes in a list or dictionary with file - timestamp.
If you want to do it for png only you can also add a line like:
if file.endswith(".png"):
before the fileToCheck line.
This should do it unless i misunderstood your question.

Change file name to date of modification

I have the following issue, I wrote a piece of code which rename file names in a directory and its sub-directories. Now instead of changing it to the current date I want it to change to the file modification date.
How can I do that?
import os, path
from datetime import datetime
import time
def walk_dir(path):
current_day = datetime.now().strftime("%Y-%m-%d")
for root, dirs, files in os.walk(path):
for filename in files:
current = root + "/" + filename
if os.path.isfile(current):
print "ORIGINAL NAME: " + current
ext = os.path.splitext(filename)[1]
target_name = os.path.join(root, '{}{}'.format(current_day, ext))
print "NEW NAME: " + target_name
os.rename(current, target_name)
walk_dir("/Users/shirin/Desktop/Artez")
import os
import datetime
for filename in directory:
modified_time = os.path.getmtime(filename) # get file modification timestamp
modified_date = datetime.date.fromtimestamp(modified_time) # convert timestamp to a date
os.rename(filename, modified_date.strftime("%Y-%m-%d"))
Note this is dangerous, since you might override files who share the same modification date.

Categories

Resources