简体   繁体   中英

"WinError 123 The filename, directory name, or volume label syntax is incorrect:" When calling os.rename

I'm working on a script that renames torrent folders to something more readable... On some occassions I get the error above.

I am really out of ideas what is wrong. I tried: os.listdir().sort() == originalFolderNames.sort(): and it came out as true so what I am trying to rename and what is in the directory matches 100%.

Here is the code that causes this:

def renameFolders(self, oldAndNewFolderNames):
    foldersToRename = oldAndNewFolderNames[1]
    originalFolderNames = oldAndNewFolderNames[0]
    dirList = os.listdir()

    #  Rename folders
    for d in dirList:
        for i in range(len(foldersToRename)):
            if d == foldersToRename[i]:
                try:
                    if foldersToRename[i] != "None":
                        os.rename(r"{}".format(str(originalFolderNames[i])), r"{}".format(str(foldersToRename[i])))
                except Exception as Err:
                    print(Err)
                    print("Couldn't rename: {} TO ==> {}".format(originalFolderNames[i], foldersToRename[i]))
                    break

    return print("Done!")

The error always occurs on the same two folders others work fine.

I really hope someone can help me with this. Others that got simmilar errors had illegal characters or didn't put in a raw string so things like "\" caused exceptions. I don't think this is the case with my script.
Tnx.

File names can't have illegal characters. List of illegal chars for linux and windows (only "/" is illegal on linux)

forbiddenChars = [">", "<", "/", ":" '"', "\\", "|", "?", "*"]

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