Home > Software design >  Text issues with Abspath
Text issues with Abspath

Time:12-10

I've been using CURRENT_DIR=os.path.dirname(os.path.abspath(__file__)) and it has been working well.

However, for whatever reason when I have any path with a /N such as

tk.PhotoImage(f'{CURRENT_DIR}\Library\Images\NA_notes.png')

or

wb.open_new(f'{CURRENT_DIR}\Library\PD\Vender\No Hold Open\DOOR.pdf')

I get:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 15-16: malformed \N character escape

It gets fixed if I just change the /N to /n

The problem is I have hundreds of these instances within a ton of folders, so is there any way to fix this without having to change every single folder name?

CodePudding user response:

If you don't want to change path then simply say to python it's raw string.

print(fr'{CURRENT_DIR}\Library\Images\NA_notes.png')

CodePudding user response:

Try escaping your location as \ is used for several escape sequence characters. You can do this by replacing \ with \\. Some thing like this:

tk.PhotoImage(f'{CURRENT_DIR}\\Library\\Images\\NA_notes.png')
wb.open_new(f'{CURRENT_DIR}\\Library\\PD\\Vender\\No Hold Open\\DOOR.pdf')
  • Related