473,471 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 18998
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.