简体   繁体   English

使用 python os.rename 时报错【183】

[英]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.这是我第一次使用 python,我一直遇到错误 183。我创建的脚本在 .network 中搜索所有“.py”文件并将它们复制到我的备份驱动器。 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. 我猜您正在Windows上运行脚本。 According to the list of windows error codes error 183 is ERROR_ALREADY_EXISTS 根据Windows错误代码列表,错误183为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. 如果您特别想覆盖文件,则可能应该先使用os.unlink删除它们。

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. 鉴于错误183为[Error 183] Cannot create a file when that file already exists的事实,即[Error 183] Cannot create a file when that file already exists ,您很可能会在os.walk()调用中找到2个同名文件,并在第一个文件成功重命名后,第二个将无法重命名为相同的名称,因此您将获得文件已存在错误。

I suggest a try/except around the os.rename() call to treat this case (append a digit after the name or something). 我建议在os.rename()调用周围使用try / except来处理这种情况(在名称或其他内容后面添加一个数字)。

[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.] [是的,我知道问这个问题已经有7年了,但是如果我从Google搜索中来到这里,也许其他地区也能找到它,这个答案可能会有所帮助。]

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.我只是遇到了同样的问题,当您尝试重命名一个文件夹时,该文件夹与同一目录中存在的文件夹具有相同的名称,Python 将引发错误。

If you trying to do that in Windows Explorer, it will ask you if you want to merge these two folders.如果您尝试在 Windows Explorer 中执行此操作,它会询问您是否要合并这两个文件夹。 however, Python doesn't have this feature.但是,Python 没有此功能。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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