简体   繁体   中英

Error [183] when using python os.rename

This is my first time using python and I keep running into error 183. The script I created searches the.network for all '.py' files and copies them to my backup drive. Please don't laugh at my script as this is my first.

Any clue to what I am doing wrong in the script?

import os
import shutil
import datetime

today = datetime.date.today()
rundate = today.strftime("%Y%m%d")

for root,dirr,filename in os.walk("p:\\"):
    for files in filename:
        if files.endswith(".py"):
            sDir = os.path.join(root, files)
            dDir = "B:\\Scripts\\20120124"
            modname = rundate + '_' + files
            shutil.copy(sDir, dDir)
            os.rename(os.path.join(dDir, files), os.path.join(dDir, modname))
            print "Renamed %s to %s in %s" % (files, modname, dDir)

I'm guessing you are running the script on windows. According to the list of windows error codes error 183 is ERROR_ALREADY_EXISTS

So I would guess the script is failing because you're attempting to rename a file over an existing file.

Perhaps you are running the script more than once per day? That would result in all the destination files already being there, so the rename is failing when the script is run additional times.

If you specifically want to overwrite the files, then you should probably delete them using os.unlink first.

Given the fact that error 183 is [Error 183] Cannot create a file when that file already exists , you're most likely finding 2 files with the same name in the os.walk() call and after the first one is renamed successfully, the second one will fail to be renamed to the same name so you'll get a file already exists error.

I suggest a try/except around the os.rename() call to treat this case (append a digit after the name or something).

[Yes, i know it's been 7 years since this question was asked but if I got here from a google search maybe others area reaching it too and this answer might help.]

I just encounter the same issue, when you trying to rename a folder with a folder that existed in the same directory has the same name, Python will raise an error.

If you trying to do that in Windows Explorer, it will ask you if you want to merge these two folders. however, Python doesn't have this feature.

Below is my codes to achieve the goal of rename a folder while a same name folder already exist, which is actually merging folders.

import os, shutil

DEST = 'D:/dest/'
SRC = 'D:/src/'

for filename in os.listdir(SRC):  # move files from SRC to DEST folder.
    try:
        shutil.move(SRC + filename, DEST)
    # In case the file you're trying to move already has a copy in DEST folder.
    except shutil.Error:  # shutil.Error: Destination path 'D:/DEST/xxxx.xxx' already exists
        pass

# Now delete the SRC folder.
# To delete a folder, you have to empty its files first.
if os.path.exists(SRC):
    for i in os.listdir(SRC):
        os.remove(os.path.join(SRC, i))
    # delete the empty folder
    os.rmdir(SRC)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM