Need help optimizing first script

Terry Reedy tjreedy at udel.edu
Thu Jun 19 16:12:27 EDT 2003


"Frederic Lafleche" <popup391 at yahoo.com> wrote in message
news:a34b632.0306190503.32483efe at posting.google.com...
> #copyacs.py
> import os, shutil, sys, time, win32file
> from win32wnet import WNetAddConnection2, error
>
> # Functions
>
> # Generate timestamp
> # OUTPUT: string in yyymmddhhmm format
> def determineFN():
>     # Get current time
>     now = time.time()
>     # Parse elements
>     (Iyear, Imonth, Iday, Ihour, Iminute) =
(time.localtime(now)[:5])
>     year = str(Iyear)
>     month = str(Imonth)
>     day = str(Iday)
>     hour = str(Ihour)
>     minute = str(Iminute)
>
>     # Padding
>     if len(month) == 1:
>         month = '0' + month
>     if len(day) == 1:
>         day = '0' + day
>     if len(hour) == 1:
>         hour = '0' + hour
>     if len(minute) == 1:
>         minute = '0' + minute
>
>     return year + month + day + hour + minute

Use the format operator to do conversions and padding.  Then above
condenses to (I believe)
   return "%4s%2s%2s%2s%2s" %  tuple(time.localtime(now)[:5])

You can use / rather than \ in DOS/Windows filenames to avoid \\ and
\\\\.

Terry J. Reedy






More information about the Python-list mailing list