Newby Question: os.rename

Kevin Bass KBASS1 at nc.rr.com
Thu Sep 12 12:04:37 CEST 2002


"Dean Goodmanson" <ponderor at lycos.com> wrote in message
news:e81be8b2.0209082039.67f33ab4 at posting.google.com...
> > >
> > > "Kevin Bass" <KBASS1 at nc.rr.com> wrote in message
> > > news:AcGc9.68214$Xa.3523883 at twister.southeast.rr.com...
> ...
> > >
> > > I have figured it out. Thanks!
> > >
> > >
> > >
>
> "CotaBas" <cotabas at earthlink.net> wrote in message
news:<fDdd9.6312$6i4.491988 at newsread1.prod.itd.earthlink.net>...
>
> >
> > LOL if you wait long enough people solve there own problems....
>
> If you're skimming or Google'ing for a similar problem...you'll find
> the problem listed 3 times and no answer in the follow-up!

Good point!!  Let me place the solution to my problem. :)

Here is the code that I used to solve this problem. I basically (1) give the
program the directory that I wanted to create the archived directoy off of,
(2) created a timestamp within a variable, (3) joined the directory with the
timestamp to form a new varaible housing the new directory, (4) created the
new directory with timestamp, (5) use glob to get a list of files, (6)
separated the directory name from the files names and placed them into
varaibles, (7) joined the new directory to the files that I glob'd, (8) then
rename the files which placed the files from one directory to another
directory.

I know that you probably did not want to know all of this but walking
through the steps helps me to undetsand any problems that I may encounter.
Like I just noticed that I have to place a try clause in the function so
that if the newly created directory is already there (the program was
executed more than once) then the program should not attempt to create the
directory again. Or I should check to see if the directory if already
present before attempting to create the directory.

def archive_data(archive):

   if archive == 'yes':
     datadir = '/home/oracle/data/'

     if os.path.isdir(datadir):
       the_date = strftime("%Y%m%d")
       archdir = os.path.join(datadir, the_date)
       os.mkdir(archdir)

       # Retrieve IBSS data from a previous inventory and archive it.
       for file in glob.glob('/home/oracle/data/*.XP'):
          dir, filename = os.path.split(file)
          oldfile = os.path.join(datadir, filename)
          newfile = os.path.join(archdir, filename)
           try:
              os.rename(oldfile, newfile)
          except:
            print "*** Unable to rename %s" %oldfile

       # Retrieve manual data from a previous inventory and archive it.
       for file in glob.glob('/home/oracle/data/*.csv'):
          dir, filename = os.path.split(file)
          oldfile = os.path.join(datadir, filename)
          newfile = os.path.join(archdir, filename)
          try:
            os.rename(oldfile, newfile)
          except:
            print "*** Unable to rename %s" %oldfile
   else:
      pass





More information about the Python-list mailing list