An ideas why I am getting an error about files not existing when running this code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buddy Alexander
    New Member
    • Jul 2010
    • 2

    An ideas why I am getting an error about files not existing when running this code?

    Code:
    import shutil, sys, time, os
    src = 'c:/users/wca36050/temp1'
    dst = 'c:/users/wca36050/temp2'
    now = time.time()
    for f in os.listdir(src):
        if os.stat(f).st_mtime < now - 7 * 86400:
            if os.path.isfile(f):
                shutil.move(f, dst)
    Error: "WindowsErr or: [Error 2] The system cannot find the file specified: 'pic.jpg'"
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Buddy,

    os.listdir() returns a list of the names of the file objects found in directory src (as in pic.jpg). os.stat(f) and os.path.isfile( f) expect the full path (as in c:/users/wca36050/temp1.pic.jpg). How else would the interpreter find the file?

    Comment

    • Buddy Alexander
      New Member
      • Jul 2010
      • 2

      #3
      Originally posted by bvdet
      Buddy,

      os.listdir() returns a list of the names of the file objects found in directory src (as in pic.jpg). os.stat(f) and os.path.isfile( f) expect the full path (as in c:/users/wca36050/temp1.pic.jpg). How else would the interpreter find the file?
      That did it! Thanks! This is all new to me.

      Code:
      for f in os.listdir(src):
          if os.stat(src + f).st_mtime < now - 7 * 86400:
              if os.path.isfile(src + f):
                  shutil.move(src + f, dst)
      Last edited by bvdet; Jul 27 '10, 02:26 AM. Reason: Fixed code tags

      Comment

      Working...