Im trying to create a program that deletes files after X days. I got that part working, but now I need to write a code that deletes the file they're located at ONLY if it's empty
This is for a server that is taking log files from builds, and those are getting placed in thousands of other folders. I need to delete those upper level folders if they are empty
import os
import shutil
import sys
import time
#for path you need to use the location of the log files
path = "C:/GroupData"
# Check current working directory.
retval = os.getcwd()
#this will list the name of the current directory
print("Current working directory %s" % retval)
# Now change the directory
#You will put the same location as you did for path
os.chdir('C:/GroupData/Temp')
workdir = os.getcwd()
# Check current working directory.
retval = os.getcwd()
#this will list the new name of the working directory
print("Directory changed successfully %s" % retval)
from subprocess import call
def get_file_directory(file):
return os.path.dirname(os.path.abspath(file))
#this code is getting what today's date is
now = time.time()
#this code is saying that files that are older than todays date by 7 days will be deleted
cutoff = now - (10 * 86400)
files = os.listdir(os.path.join(get_file_directory('C://GroupData//Temp'), "temp"))
file_path = os.path.join(get_file_directory('C://GroupData//Temp'), "temp/")
#this locates what the file name is and looks to see how long ago it was modified
for xfile in files:
files1 = os.listdir(os.path.join(get_file_directory('C://GroupData//Temp//' + xfile), xfile))
file_path1 = os.path.join(get_file_directory('C://GroupData//Temp//' + xfile), xfile)
for xfile1 in files1:
if os.path.isfile(str(file_path1) + "\\" + xfile1):
t = os.stat(str(file_path1) + "\\" + xfile1)
#m is how long ago it was last modified
m = t.st_mtime
#if the cutoff date is older than the modified date the file will be deleted
if m < cutoff:
#if the file IS older than the cutoff os.remove deletes the file from the path
os.remove(str(file_path1) + "\\" + xfile1)
files = os.listdir(os.path.join(get_file_directory('C://GroupData//Temp'), "temp"))
file_path = os.path.join(get_file_directory('C://GroupData//Temp'), "temp/")
os.rmdir(str(file_path1) + "\\")
The code at the bottom works, but only for one file at a time, and I need it to do it as many times as possible so that it can delete all the empty files at once, as this will be run automatically
Related
I have a folder that contains about 5,000 files and I need to move the files that start with certain names to another folder. The issue is that I need to only take the file with the most recent created date for that name.
For example:
Let's say one of the names I've been given is "Sam Thompson" and there are three files that meet that criteria...
Sam_Thompson_02012023
Sam_Thompson_02052023
Sam_Thompson_02102023 (let's assume this is the most recently created file)
I need to move only the last file to a different folder as it was created most recently.
I have figured out how to move files based on the name piece but have struggled with finding resources online that relate to my issue very closely.
The below code works perfectly -- it moves the files that start with Sam_Thompson to the appropriate folder. The part that doesn't work is the part that's commented out: and max(file_name, key=os.path.getctime):
I think I'm going about getting the max date by file name incorrectly so any input is appreciated. Most of the material I've looked at online is specific to scenarios where you just want to return the name of the latest file (not move it) and it's typically looking across the whole folder not by each given name.
import shutil
import os
source_folder = 'insert path'
destination_folder = 'insert path'
s = 'Sam_Thompson'
for file_name in os.listdir(source_folder):
source = source_folder + file_name
destination = destination_folder + file_name
if os.path.isfile(source) and file_name.startswith(s): #and max(file_name, key=os.path.getctime):
shutil.copy(source, destination)
print('Copied:', file_name)
The issue is that your file_name is only looking at one file at a time. You could store the date and then check if subsequent files have a more recent date to find it, but it's easier just to pull all the valid files at once with list comprehension.
source_folder = ''
destination_folder = ''
s = ''
valid_files = [file for file in os.listdir(source_folder) if file.startswith(s)]
most_recent = max(valid_files, key = lambda x: os.path.getctime(os.path.abspath(source_folder) + os.sep + x))
source = source_folder + os.sep + most_recent
destination = destination_folder + os.sep + most_recent
shutil.copy(source, destination)
I'm trying to delete a file in a certain folder. I tried to use this command:
file_exists = os.path.exists(line1[0] + '.xlsx')
if file_exists:
find_file = m.find(line1[0] + ".xlsx")
if find_file:
delete_file = m.delete(find_file[0])
The problem is there are multiple files with the same name in different folders. The folder's name is the date that the folder is created. The file name is the name of a course code, eg. BH2952. When I use the above command, all the files that has the name BH2952 are deleted. But I only want to delete the file in today's date folder. Does anyone know how to do this?
And here is the full code that I've done:
import os
import os.path
import sys
from pathlib import Path
from os.path import exists
from datetime import datetime
today = datetime.now()
from mega import Mega
mega = Mega()
# Login to MEGA
m = mega.login('nurul.syamsina1202#gmail.com', 'Syamsina990212')
# Get user details
details = m.get_user()
# Get account disk quota
quota = m.get_quota()
# Get account storage space
''' specify unit output kilo, mega, gig, else bytes will output '''
space = m.get_storage_space(kilo=True)
# Get accounts file
files = m.get_files()
# Create a folder on Mega if the file hasn't been created yet
''' Excludes results which are in the Trash folder (i.e. deleted) '''
folder = m.find("Fourth_Year_Students", exclude_deleted=True)
if not folder:
m.create_folder("Fourth_Year_Students")
subfolder = m.find("Fourth_Year_Students/" + today.strftime('%d%m%Y'), exclude_deleted=True)
if not subfolder:
m.create_folder("Fourth_Year_Students/" + today.strftime('%d%m%Y'))
# Change directory to today's date folder
os.chdir(r"C:/OpenVino/excel_report/Fourth_Year_Students/" + today.strftime('%d%m%Y'))
os.getcwd()
# read class schedule file
file1 = open(r"C:/OpenVino/excel_report/class_codes_and_names.txt", "r")
lines_1 = file1.readlines()
for line1 in lines_1:
line1 = line1.strip('\n')
line1 = line1.split(",")
#os.chdir(directory_name)
file_exists = os.path.exists(line1[0] + '.xlsx')
if file_exists:
find_file = m.find(line1[0] + ".xlsx")
if find_file:
delete_file = m.delete(find_file[0])
# Upload a file and get its public link
folder = m.find("Fourth_Year_Students/" + today.strftime('%d%m%Y'))
file = m.upload(line1[0] + '.xlsx', folder[0])
link = m.get_upload_link(file)
print('\nFile',line1[0],'is ready. To view the file, please click on the link below:\n',link)
# see mega.py for destination and filename options
else:
continue
m.empty_trash()
print("All files have been successfully uploaded in the cloud.")
This is my first question here on Stack overflow. I am very new in programming area and after going though some of the videos and links , I start to write my python program which I learn from google.
Here I am trying to run a program where I need to delete the folder in particular path(Starting with name CXP) which is 7 days old.
But here is two problems.
Deleting a folder using shutil is actually deleting the Main folder b instead of the folder inside the b which start with CXP.
Thanks for this stack over flow as i am learning and practicing from here.
import os, time
import shutil
dir_name = os.path.expanduser("") + "/proj/a/b/"
test = os.listdir(dir_name)
now = time.time()
print(test)
for fname in os.listdir(dir_name):
if fname.startswith("CXP"):
folderstamp = os.stat(os.path.join(dir_name, fname)).st_mtime
foldercompare = now - 7 * 86400
if folderstamp < foldercompare:
shutil.rmtree(dir_name)
Your script is deleting the main folder because this is what is implemented. Note that you are doing checks based on fname, but later use shutil.rmtree(dir_name) if the condition is met. You need to correct the argument given to shutil.rmtree:
import os
import time
import shutil
dir_name = os.path.expanduser("") + "/proj/a/b/"
test = os.listdir(dir_name)
now = time.time()
print(test)
for fname in os.listdir(dir_name):
if fname.startswith("CXP"):
folder = os.path.join(dir_name, fname)
folderstamp = os.stat(folder).st_mtime
foldercompare = now - 7 * 86400
if folderstamp < foldercompare:
shutil.rmtree(folder)
i'm pretty new to Python and i tried writing a code that takes a list of files (1.pdf,2.pdf,3.pdf etc') and renames it to the time it was created.
I'm using Ubuntu 21 and python 3.8
import time
import os
extension = ".pdf"
folder_path = "/home/user/Downloads"
file_list = []
for filename in os.listdir(folder_path):
if filename.endswith(extension) and filename not in file_list:
full = folder_path + '/' + filename
t = os.path.getctime(full)
t_str = time.ctime(t)
t_obj = time.strptime(t_str)
format_t = time.strftime("%Y%m%d_%H%M%S", t_obj)
full_path = os.path.join(folder_path, filename)
os.rename(full_path, os.path.join(folder_path, format_t + extension))
name_sum = format_t + extension
file_list.append(name_sum)
When i run the code files are deleted and only 1 file is left.
Please help me understand what am i doing wrong.
They are all renamed to the same name. You should add a check like:
new_name = os.path.join(folder_path, format_t + extension)
if os.path.isfile(new_name):
raise Exception(new_name + " exists already")
os.rename(full_path, new_name)
or handling files that were created at the exact same second by adding some suffix or something like that
Your code seems correct and worked correctly on my system.
You could try adding an iterator counter number to differentiate the names in the loop. The uniqueness of the names may be caused of the "format_t" you add producing the same within the loop may be cause of the execution time or an internal bug within the time function yielding the same strings.
I'm currently looking to create a directory on Linux using Python v2.7 with the directory name as the date and time (ie. 27-10-2011 23:00:01). My code for this is below:-
import time
import os
dirfmt = "/root/%4d-%02d-%02d %02d:%02d:%02d"
dirname = dirfmt % time.localtime()[0:6]
os.mkdir(dirname)
This code works fine and generates the directory as requested. Nonetheless, what I'd also like to then is, within this directory create two csv files and a log file with the same name. Now as the directory name is dynamically generated, I'm unsure as to how to move into this directory to create these files. I'd like the directory together with the three files to all have the same name (csv files will be prefixed with a letter). So for example, given the above, I'd like a directory created called "27-10-2011 23:00:01" and then within this, two csv files called "a27-10-2011 23:00:01.csv" and "b27-10-2011 23:00:01.csv" and a log file called "27-10-2011 23:00:01.log".
My code for the file creations is as below:-
csvafmt = "a%4d-%02d-%02d %02d:%02d:%02d.csv"
csvbfmt = "b%4d-%02d-%02d %02d:%02d:%02d.csv"
logfmt = "%4d-%02d-%02d %02d:%02d:%02d.log"
csvafile = csvafmt % time.localtime()[0:6]
csvbfile = csvbfmt % time.localtime()[0:6]
logfile = logfmt % time.localtime()[0:6]
fcsva = open(csvafile, 'wb')
fcsvb = open(csvbfile, 'wb')
flog = open(logfile, 'wb')
Any suggestions how I can do this so that the second remains the same throughout? I appreciate this code would only take a split second to run but within that time, the second may change. I assume the key to this lies within altering "time.localtime" but I remain unsure.
Thanks
Sure, just save the time in a variable and then use that variable for the substitutions:
now = time.localtime()[0:6]
dirname = dirfmt % now
csvafile = os.path.join(dirname, csvafmt % now)
csvbfile = os.path.join(dirname, csvbfmt % now)
logfile = os.path.join(dirname, logfmt % now)
Edited to include creating the complete path to your csv and log files.
Only call time.localtime once.
current_time = time.localtime()[0:6]
csvafile = csvafmt % current_time
csvbfile = csvbfmt % current_time
logfile = logfmt % current_time