0

I am trying to read multiple file in my personal computer with glob.glob as below:

diri  = r"C:\Users\Hoonill\Desktop\Python\BAO\2007\2011"
filin = diri + '\*10*datresult'
FileList=sorted(glob.glob(filin))
print(FileList)

my expected result was such as the series of

'C:\Users\Hoonill\Desktop\Python\BAO\2007\2011\BAO_100_2011229.datresult',

but what I got was the series of

'C:\\Users\\Hoonill\\Desktop\\Python\\BAO\\2007\\2011\\BAO_100_2011229.datresult'

I don't know why '\' become '\\'.

Based on other people's advice, this result seems due to addition of 'r' on file path, but without 'r', I got "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape."

Any idea or help will be really appreciated.

Best regards,

Isaac

Isaac
  • 885
  • 2
  • 15
  • 35
  • 1
    another example of where this has been answered: http://stackoverflow.com/questions/11924706/how-to-get-rid-of-double-backslash-in-python-windows-file-path-string – Rob Imig Jan 26 '16 at 21:33
  • Thank you Rob, the problem is that I should use 'r', because of eol string error. – Isaac Jan 26 '16 at 21:44

1 Answers1

1

That is Python escaping the \ characters in the Windows file path. It will print normally if you use the print() function/statement (depending on your Python version):

>>> lst = ['C:\Users\Hoonill\Desktop\Python\BAO']
>>> lst
['C:\\Users\\Hoonill\\Desktop\\Python\\BAO']
>>> for ele in lst:
...     print(ele)
...
C:\Users\Hoonill\Desktop\Python\BAO
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
  • Thank you IanAuld, but it doesn't seem to work for me. Based on other's opinion, it seems due to add 'r'. However, without 'r', I have "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape" – Isaac Jan 26 '16 at 21:48