0

I have a script that should rename the file default.xex to the value of FileName[:-1] which is the name of the directory the xex file is in. The code I have is:

`

# This renames default.xex to the correct FileName.xex
    for filename in DirectoryList:
        path = HomePath + UnpackedPath + FileName + FileName + FileName + FileName
        filename = FileName[:-1] + "\\default.xex"
        src = filename
        dst = FileName[:-1] + ".xex"
        os.rename(os.path.join(path, src), os.path.join(path, dst))

`

however this always throws an error showing \FileName.xex when \FileName.xex is expected. The error is:

Exception has occurred: FileNotFoundError [Errno 2] No such file or directory: '/home/corey/XBLA_Unpacked/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon\default.xex' -> '/home/corey/XBLA_Unpacked/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon/FarCry 3 Blood Dragon.xex'

This renames default.xex to the correct FileName.xex

for filename in DirectoryList:
    path = HomePath + UnpackedPath + FileName + FileName + FileName + FileName
    filename = FileName[:-1] + "\\default.xex"
    src = filename
    dst = FileName[:-1] + ".xex"
    os.rename(os.path.join(path, src), os.path.join(path, dst))

Edit: I just realized this only shows 1 , however on my screen in VS Code and in this text editor it shows 2

the original xex file is FarCry 3 Blood Dragon\default.xex.

I have tried every solution I could find while googling and looking at other people's solutions on here and nothing seems to work. I tried setting it as a specific string variable and running it that way and it still resulted in \.

cmclark00
  • 15
  • 5
  • 1
    You say `this always prints two`. Nowhere in your code are you printing anything. Please make a code example that reproduces the problem you are having. – Mark Oct 26 '22 at 04:07
  • I have added more information per your comment. it doesnt print two, but throws and error message that shows two – cmclark00 Oct 26 '22 at 04:39
  • Probably same root issue as [this](https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice). – Ture Pålsson Oct 26 '22 at 04:52
  • I read through a lot of those posts. Using r"\defult.xex" seemed promising but alas it still looked for two \ as shown in the error message – cmclark00 Oct 26 '22 at 12:02

1 Answers1

1

The error message is showing the repr of the string, in order to be unambiguous if there are any "strange" characters in there. There is only one backslash in the string, but just as it has to be doubled when it occurs in a string literal, it will be doubled in the output, because a single backslash would be used for special characters.

For example, if I run the following program

import os
f='a\\b'
print(len(f))
os.rename(f, 'foo')

it prints 3, showing that the string is just 3 characters, but then it gives the error message

FileNotFoundError: [Errno 2] No such file or directory: 'a\\b' -> 'foo'
Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15