Newby Question: os.rename

Kevin Bass KBASS1 at
Mon Sep 2 05:30:08 EDT 2002

I am attempting to create an archive function within my program. This
function should create a directory with a timestamp name (such as
/home/oracle/data/20020831) on the fly, list the contents of a current
directory then place the contain from the previous directory into the newly
created directory.

The newly created directory with the timestamp name is being created and the
list of files from the current directory has been retrieved but my problem
comes when moving the files to the newly created directory. I am trying to
achieve this tasks with os.rename but find it difficult to get the command
to recognize the new directory and place the files into it.

I have attempted to get os.rename to recognize the 'archloc' variable that
contains the name of the newly created directory but that did not work.  I
also tried to join the newly created directory with the file names then send
it through os.rename and that did not work either.
What am I doing wrong? Thanks!! (My code is listed below)

def archive_data(archive):

   the_date = strftime("%Y%m%d")
   archloc = ('/home/oracle/data/' + the_date)

   if archive == 'yes':
      for file in os.listdir('/home/oracle/data'):
         file2 = os.path.join(archloc, file)
         #print file2
   os.rename(r'/home/oracle/data/' + file, file2)


More information about the Python-list mailing list