Change file name to date of modification - python

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.

Related

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

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')

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)))

Change filename for multiple files

I want to change filename for all my files in a folder. They all end with a date and time like "filename 2019-05-20 1357" and I want the date first for all files. How can I do that simplest way?
#!/usr/bin/python3
import shutil, os, re
r = re.compile(r"^(.*) (\d{4}-\d{2}-\d{2} \d{4})$")
for f in os.listdir():
m = r.match(f)
if m:
shutil.move(f, "{} {}".format(m.group(2), m.group(1)))
Quick and roughly tested version
Here is my Implementation:
from datetime import datetime
import os
path = '/Users/name/desktop/directory'
for _, file in enumerate(os.listdir(path)):
os.rename(os.path.join(path, file), os.path.join(path, str(datetime.now().strftime("%d-%m-%Y %H%M"))+str(file)))
Output Format:
20-05-2019 1749filename.ext
import os
import re
import shutil
dir_path = '' # give the dir name
comp = re.compile(r'\d{4}-\d{2}-\d{2}')
for file in os.listdir(dir_path):
if '.' in file:
index = [i for i, v in enumerate(file,0) if v=='.'][-1]
name = file[:index]
ext = file[index+1:]
else:
ext=''
name = file
data = comp.findall(name)
if len(data)!=0:
date= comp.findall(name)[0]
rest_name = ' '.join(comp.split(name)).strip()
new_name = '{} {}{}'.format(date,rest_name,'.'+ext)
print('changing {} to {}'.format(name, new_name))
shutil.move(os.path.join(dir_path,name), os.path.join(dir_path, new_name))
else:
print('file {} is not change'.format(name))

Organizing data by filetype

I am trying to sort a large number of files based off of their file extension. A lot of the files are .doc, .docx, .xls, etc.
This is what I was thinking in my head, but if there is a simpler way to do things, let me know! I do have multiple files with the same extension, so I don't want it to create a new folder for that extension every time and overwrite the previous file. I also have a much larger list, but for this example I don't believe all of them are needed. The OS is MacOS.
import os, shutil
extList = ['.doc', '.docx', '.xls']
for ext in extList:
os.mkdir(path + '/' + ext +'_folder')
for file in os.listdir(filepath):
if file.endswith(ext): #missing an indent
print(file)
shutil.copyfile(file + '/' + ext +'_folder' + file)
Also, if I run into a file that I do not have on my list, I would like it to go into a folder named 'noextlist'.
Here is what I was able to create quickly
import os, re, shutil
DocFolder = r'...'#Your doc folder path
DocxFolder = r'...'#Your docx folder path
XlsFolder = r'...'#Your xls folder path
MiscFolder = r'...'#Your misc folder path
for root, dirs, files in os.walk(r'...'): #Your folder path you want to sort
for file in files:
if file.endswith(".doc"):
sourceFolder = os.path.join(root,file)
print sourceFolder
shutil.copy2(sourceFolder,DocFolder)
elif file.endswith(".docx"):
sourceFolder = os.path.join(root,file)
print sourceFolder
shutil.copy2(sourceFolder,DocxFolder)
elif file.endswith(".xls"):
sourceFolder = os.path.join(root,file)
print sourceFolder
shutil.copy2(sourceFolder,XlsFolder)
else:
sourceFolder = os.path.join(root,file)
print sourceFolder
shutil.copy2(sourceFolder,MiscFolder)
Edit:The main function here is the for root,dirs,files in os.walk This allows the program to transverse through the provided path to search all files including the ones in the sub folder and sort it out accordingly.
import errno
import shutil
from os import listdir, mkdir
from os.path import splitext, join
# set for fast lookup
extList = set(['.doc', '.docx', '.xls'])
# source path
filepath = ...
# dest path
path = ...
for f in listdir(filepath):
# extract extension from file name
ext = splitext(f)[1]
if ext in extList:
dir_ = join(path, "{}_folder".format(ext))
try:
mkdir(dir_)
except OSError as e:
if ex.errno != errno.EEXIST:
raise # raise if any other error than "already exists"
dest = join(dir_, f)
else:
dest = join(path, "noextlist_folder", f)
shutil.copy2(join(filepath, f), dest)
If I understand correctly, you like your solution but you need a way to rename files with duplicate names so that the extras don't disappear. You can check if the destination file already exists and construct a variant name by adding _1, _2, etc. to the filename until you find something unused.
newpathname = path + '/' + ext +'_folder' + "/" + file
n = 0
while os.path.exists(newpathname):
n += 1
base, ext = os.path.splitext(newpathname)
newpathname = "%s_%d%s" % (base, n, ext)
shutil.copyfile(filepath+"/"+file, newpathname)
But your code has some other glitches, so here's a rewritten scanner. It uses os.walk() to descend into several levels of subdirectories (you don't say if that's needed or not), and it collects files of all extensions in one pass. And it constructs variant names as before.
import os, shutil
extList = ['.doc', '.docx', '.xls']
from os.path import join as joinpath
# Make sure the destination directories exist
for ext in extList:
extdir = joinpath(path, ext[1:]+"_folder")
if not os.path.exists(extdir):
os.mkdir(extdir)
for dirname, _dirs, files in os.walk(filepath):
for file in files:
base, ext = os.path.splitext(file)
if ext not in extList:
continue
destpath = joinpath(path, ext[1:]+"_folder")
n = 0
newpathname = joinpath(destpath, file)
# If the new name is in use, find an unused variant
while os.path.exists(newpathname):
n += 1
newfile = "%s_%d%s" % (base, n, ext)
newpathname = joinpath(path, newfile)
sh.copy(joinpath(dirname, file), newpathname) # or other copy method

Pulling Files and Timestamps from a Directory and Subdirectories

I have a working script that will print all files in a given directory. I would like help making it do two additional things:
(1) Also be able to print the date_created or time stamp for each file.
(2) Do all of the above not only for files in the given directory, but in all subdirectories as well.
Here is the working script:
from os import listdir
from os.path import isfile, join
from sys import argv
script, filename = argv
mypath = os.getcwd()
allfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
output = open(filename, 'w')
for i in allfiles:
string = "%s" %i
output.write(string + "\n")
output.close()
print "Directory printed."
I would hope to be able to print something like (filename + ", " + timestamp + "\n"), or some substitute.
Thanks!
http://docs.python.org/2/library/os.html and http://docs.python.org/2/library/stat.html have you covered.
os.walk will give you the recursive directory walking
stat will give you file timestamps (atime,ctime,mtime)
This snippet walks through files in a directory + subdirectories and prints out created and modified timestamps.
import os
import time
def walk_files(directory_path):
# Walk through files in directory_path, including subdirectories
for root, _, filenames in os.walk(directory_path):
for filename in filenames:
file_path = root + '/' + filename
created = os.path.getctime(file_path)
modified = os.path.getmtime(file_path)
# Process stuff for the file here, for example...
print "File: %s" % file_path
print " Created: %s" % time.ctime(created)
print " Last modified: %s" % time.ctime(modified)
walk_files('/path/to/directory/')

Categories

Resources