shutil.move() Error ([Errno 17] File exists:) on win32

Discussion in 'Python' started by bmgz, Dec 16, 2003.

  1. bmgz

    bmgz Guest

    I am have made a simple script that moves all desktop clutter (ie files that
    are not *.lnk) to a specified folder
    eg. c:\myhome\mydocs\desktopdebris\2003-12-16

    ----------------------------------------------------------------------------
    -----------------
    import re, os, time, shutil
    os.chdir(os.environ['HOMEDRIVE']+os.environ['HOMEPATH']+"\\Desktop")
    DESKTOP =
    os.listdir(os.environ['HOMEDRIVE']+os.environ['HOMEPATH']+"\\Desktop")
    TODAYDIR = "E:\\My Documents\\desktop_debris\\"+time.strftime('%Y-%m-%d')


    if not os.path.exists(TODAYDIR):
    os.mkdir(TODAYDIR)

    for i in range(len(DESKTOP)):
    if not re.search("\.lnk$", DESKTOP):
    shutil.move(DESKTOP,TODAYDIR)
    ----------------------------------------------------------------------------
    -----------------------

    this works fine for FILES on the desktop, but when a FOLDER is present the
    script exits with the following error:

    Traceback (most recent call last):
    File "E:\My Documents\python\cleanup_desktop.py", line 12, in ?
    shutil.move(DESKTOP,TODAYDIR)
    File "C:\Program Files\Python-2.3\lib\shutil.py", line 167, in move
    copytree(src, dst, symlinks=True)
    File "C:\Program Files\Python-2.3\lib\shutil.py", line 101, in copytree
    os.mkdir(dst)
    OSError: [Errno 17] File exists: 'E:\\My
    Documents\\desktop_debris\\2003-12-16'


    It seems that the shutil.move() function is trying to recreate the target
    directory? Is their another module one could use in this case?
    (shutil.copy() then delete() also gives problems - then again that IS
    move()..)
     
    bmgz, Dec 16, 2003
    #1
    1. Advertising

  2. bmgz

    Hans Nowak Guest

    bmgz wrote:
    > I am have made a simple script that moves all desktop clutter (ie files that
    > are not *.lnk) to a specified folder
    > eg. c:\myhome\mydocs\desktopdebris\2003-12-16
    >
    > ----------------------------------------------------------------------------
    > -----------------
    > import re, os, time, shutil
    > os.chdir(os.environ['HOMEDRIVE']+os.environ['HOMEPATH']+"\\Desktop")
    > DESKTOP =
    > os.listdir(os.environ['HOMEDRIVE']+os.environ['HOMEPATH']+"\\Desktop")
    > TODAYDIR = "E:\\My Documents\\desktop_debris\\"+time.strftime('%Y-%m-%d')
    >
    >
    > if not os.path.exists(TODAYDIR):
    > os.mkdir(TODAYDIR)
    >
    > for i in range(len(DESKTOP)):
    > if not re.search("\.lnk$", DESKTOP):
    > shutil.move(DESKTOP,TODAYDIR)
    > ----------------------------------------------------------------------------
    > -----------------------
    >
    > this works fine for FILES on the desktop, but when a FOLDER is present the
    > script exits with the following error:
    >
    > Traceback (most recent call last):
    > File "E:\My Documents\python\cleanup_desktop.py", line 12, in ?
    > shutil.move(DESKTOP,TODAYDIR)
    > File "C:\Program Files\Python-2.3\lib\shutil.py", line 167, in move
    > copytree(src, dst, symlinks=True)
    > File "C:\Program Files\Python-2.3\lib\shutil.py", line 101, in copytree
    > os.mkdir(dst)
    > OSError: [Errno 17] File exists: 'E:\\My
    > Documents\\desktop_debris\\2003-12-16'


    I'm not sure, but I think it kinda works like this.

    When you move a file, you can do:

    shutil.move(filename, directory)

    e.g.

    shutil.move('test.txt', 'c:/temp')

    This moves the file to the c:/temp directory. Nothing surprising here.

    However, when moving a directory, this is a bit different. shutil.move(dir1,
    dir2) doesn't work if dir2 already exists. Rather, you need to specify the
    *new name* of the directory:

    shutil.move('c:/dir1', 'c:/temp')
    # WRONG if c:/temp already exists

    shutil.move('c:/dir1', 'c:/temp/dir1')
    # does what you want

    HTH,

    --
    Hans ()
    http://zephyrfalcon.org/
     
    Hans Nowak, Dec 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Chase
    Replies:
    1
    Views:
    1,520
    Sean DiZazzo
    Jul 13, 2009
  2. Fletcher Johnson

    Why is the shutil module called shutil?

    Fletcher Johnson, Sep 24, 2011, in forum: Python
    Replies:
    11
    Views:
    912
    alex23
    Sep 26, 2011
  3. Brandon Simmons
    Replies:
    0
    Views:
    383
    Brandon Simmons
    Dec 22, 2010
  4. Ziv Tepman
    Replies:
    0
    Views:
    88
    Ziv Tepman
    Jan 21, 2014
  5. Ziv Tepman
    Replies:
    0
    Views:
    108
    Ziv Tepman
    Jan 21, 2014
Loading...

Share This Page