0

I have a question on correctly decode a window path in python. I tried several method online but didn't find a solution. I assigned the path (folder directory) to a variable and would like to read it as raw. However, there is '\' combined with number and python can't read correctly, any suggestion? Thanks

fld_dic = 'D:TestData\20190917_DT19_HigherFlowRate_StdCooler\DM19_Data'

I would like to have:

r'D:TestData\20190917_DT19_HigherFlowRate_StdCooler\DM19_Data'

And I tried: fr'{fld_dic}' it gives me answer as: 'D:TestData\x8190917_DT19_HigherFlowRate_StdCooler\\DM19_Data' which is not what I want. Any idea how to change to raw string from an assigned variable with '\' and number combined? Thanks

Capie
  • 976
  • 1
  • 8
  • 20
gcgogo
  • 25
  • 5
  • scape the \ with a / before such as /\ – Capie May 19 '20 at 14:14
  • many thanks for your help. However, this is not the answer I am looking for. My problem is when i assign a window path which contains back slash and number combination like '\202' to a parameter in a function. The parser will automatically change '\201' to '\x81' utf code. Is there a way to stop the parser to change it during the assignment? Assume I just copy the window path directly and assign it to the function parameter. like def fcn(path= 'C:\202\data'): for example. Thanks. – gcgogo May 21 '20 at 00:02

1 Answers1

0

The problem's root caused is string assigning. When you assigning like that path='c:\202\data' python encode this string according to default UNICODE. You need to change your assigning. You have to assige as raw string. Also like this path usage is not best practice. It will occure proble continuesly. It is not meet with PEP8

You should not be used path variable as string. It will destroy python cross platform advantage.

You should use pathlib or os.path. I recommend pathlib. It have pure windows and linux path. Also while getting path use this path. If You get path from and input you can read it as raw text and convert to pathlib instance.

Check this link: https://docs.python.org/3/library/pathlib.html

It works but not best practice. Just replace path assigning as raw string/

import os

def fcn(path=r'C:\202\data'):
    print(path)
    os.chdir(path)

fcn()
Aziz F Dagli
  • 304
  • 2
  • 10
  • Thanks for your help. I am still not quite clear what to do here. Hope you would be providing an answer using the pathlib . I simply want to define a function with input argument of windows path def fnc(fld_dic). Then inside the function, I would like to list all the files of this directory in a list using os.listdir(fld_dic). And fld_dic is directory above. it keeps giving me the error because \201 will become \x81. Would you be able to show me how to achieve this?? BTW, I also don't understand why \201 becomes \x81. I though \x81 is str of '129'. Thanks. – gcgogo May 19 '20 at 15:32
  • I added a sample code to my answer. I hope I will help you, – Aziz F Dagli May 19 '20 at 20:38
  • many thanks for your help. However, this is not the answer I am looking for. My problem is when i assign a window path which contains back slash and number combination like '\202' to a parameter in a function. The parser will automatically change '\201' to '\x81' utf code. Is there a way to stop the parser to change it during the assignment? Assume I just copy the window path directly and assign it to the function parameter. like def fcn(path= 'C:\202\data'): for example. Thanks. – gcgogo May 20 '20 at 23:57
  • if you want use like thaht `def fcn(path= 'C:\202\data')` it is not possible to use like that because `\` this is escape character. An escape character is a particular case of metacharacters. You should change your code. ``def fcn(path= 'C:\202\data'.encode('unicode_escape'))`` like that. [Similar Topic ](https://stackoverflow.com/a/13967409/10972883) – Aziz F Dagli May 22 '20 at 05:13
  • Thanks. But one more question. Is it possible to encode the variable ‘path’ inside the function? like def fcn(path = ‘C:\202\data’): then inside the function put something like path_encode = path.encode(‘unicode_escape’) my understanding is this is not possible. After assigning to a variable, the parsing will already be done. Would you mind helping to confirm if my understanding is correct? Thanks – gcgogo May 23 '20 at 12:45
  • Yes friend it is possible. you can use inside of function. I updated my answer again as you say. – Aziz F Dagli May 23 '20 at 22:14
  • Hi, thanks again for being so patient. This is exactly my problem. I tried the same code you updated. If I print the parh_encode inside the function, it gives b’C:\\x82\\\\Data’ then if you use os.chdir(path_encode). It complains can’t find this folder. I don’t know if you have tried to run this function. I am using python 3.8 on windows 10. Thanks – gcgogo May 25 '20 at 01:19
  • def fcn(path=‘C:\202\data’): path_encode=path.encode(‘unicode_escape’) os.chdir(parh_encode) Just like this, simple test to see how I want with windows directory. But it seems doesn’t work as this. Complains can’t find the directory if you call the function. – gcgogo May 25 '20 at 15:53
  • The problem's root caused is string assigning. When you assigning like that `path='c:\202\data'` python encode this string according to default UNICODE. You need to change your assigning. You have to assige as raw string `path=r"C:\202\data"`. Also like this path usage is not best practice. It will occure proble continuesly. It is not meet with PEP8. I updated answer again. – Aziz F Dagli May 26 '20 at 08:20
  • Thanks for the great help!! – gcgogo May 26 '20 at 12:38