a problem in datetime.interval()

Andre Engels andreengels at gmail.com
Mon Mar 9 11:14:58 CET 2009


On Mon, Mar 9, 2009 at 10:57 AM, lameck kassana <lkassana at gmail.com> wrote:
> hi ,
> I am writing a scriot which will move old files from one directory to backup
> The files are in mode *20090307* mean *yyyymmmdd* to new files.
> The script is below is like this
>
> import os
> import datetime
> import time
> from datetime import date
> today=date.today()
> for x in range(5,10):
>     interval=datetime.timedelta(x)
>     print interval
>     today=today-interval
>     today=today.strftime("%Y%m%d")
>     print today
>     src=r"f:\Sql_Loader\SMR\file_log_backup\smr"+today+"*"
>     dest=r" f:\Sql_Loader\SMR\file_log_backup2"
>     print src
>     print dest
>     os.system(r"move " +src+" "+ dest)
>
>
>
> but the error is like that
> Traceback (most recent call last):
>   File "remove_old_files.py", line 10, in <module>
>     today=today-interval
> TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'
>
> It seems datetime.timedelta()does not respond with my loop
> Any help??

The first time you go through the loop, all goes well, but during that
loop there is the command

today=today.strftime("%Y%m%d")

After this, today is not a date any more, but a string - and you
cannot substract a timedelta from a string.


--
André Engels, andreengels at gmail.com



More information about the Python-list mailing list