473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Newby Question: Remove files older than 7 days from a directory

I would like to remove file that are older than 7 days old from a directory.
I can do this in shell script rather easy but I would like to integrate this
functionality into my Python program. How can this be achieved? Which module
can be used to perform this tasks? Thanks!

Shell Script example: find /path/to/dir -mtime +30 -exec rm '{}' \;
Kevin
Jul 18 '05 #1
2 18993
kbass wrote:
I would like to remove file that are older than 7 days old from a directory.
I can do this in shell script rather easy but I would like to integrate this
functionality into my Python program. How can this be achieved? Which module
can be used to perform this tasks? Thanks!

Shell Script example: find /path/to/dir -mtime +30 -exec rm '{}' \;


Here's a short example:

import os, time

path = r"c:\tmp"
now = time.time()
for f in os.listdir(path):
if os.stat(f).st_mtime < now - 7 * 86400:
if os.path.isfile(f):
os.remove(os.path.join(path, f))

-- Gerhard

Jul 18 '05 #2
"kbass" <kb***@midsouth.rr.com> wrote in message news:<i2*******************@clmboh1-nws5.columbus.rr.com>...
I would like to remove file that are older than 7 days old from a directory.
I can do this in shell script rather easy but I would like to integrate this
functionality into my Python program. How can this be achieved? Which module
can be used to perform this tasks? Thanks!

Shell Script example: find /path/to/dir -mtime +30 -exec rm '{}' \;
Kevin


Well, one way would be

import os
os.system("find /path/to/dir -mtime +30 -exec rm '{}' \;")

but that's cheating (and non-portable, of course).

Gerhard's code is a great example, but it won't recurse into
subdirectories like the find command does. For that you need the
os.path.walk() or (in Python 2.3) the os.walk() function. The
os.path.walk() version is notorious for its counterinuitiveness, hence
the newer, friendlier version in the os module.

I'd gladly recommend Joe Orendorff's 'path' module, which doesn't come
with the Python Standard Library, but which you can download and
include either in your site-packages directory, or just put it in the
same directory as your application. The path module takes a more
object-oriented approach than the walk() variants in the standard
library. Example:

import time
from path import path

seven_days_ago = time.time() - 7 * 86400
base = path('/path/to/dir')

for somefile in base.walkfiles():
if somefile.mtime < seven_days_ago:
somefile.remove()
The path module is available at
http://www.jorendorff.com/articles/python/path/

-- Graham
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Anand K Rayudu | last post by:
Hi all, I am trying to find a way to get the files recursively in a given directory, The following code is failing, can some one please suggest what could be problem here from os import...
2
by: Matt | last post by:
Hello, I am writing a script that opens needs to get a listing of files in a directory, print that listing to a file and use that file as a quasi ftp control file. My problem is that when I...
2
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
10
by: Charles Russell | last post by:
Why does this work from the python prompt, but fail from a script? How does one make it work from a script? #! /usr/bin/python import glob # following line works from python prompt; why not in...
2
by: express99 | last post by:
i am new to perl scripting. i want to be able to delete folders and files older than n days recursively in a windows environment.
1
by: MaFt | last post by:
hi all, i'm trying to automate the removal of files in a temp directory that are more than a day old. at present i manually go in and delete them twice or three times a week. the code i have at...
3
by: jaeden99 | last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with...
0
by: Arulmanoj | last post by:
Hi, I need a batch file to delete all PDF files in a folder and it's subfolders. I will be giving the folder path and number of old days to delete as static values.. Please it's very urgent. Also...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.