1

I want to include Windows paths in python script comments, without causing an encoding error.

If I include a Windows path in a comment, I will sometimes get an encoding error, e.g., "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa6 in position 4612: invalid start byte".
I found one "article" which indicated that including a Windows path in a comment can trigger a unicode error, https://programmersought.com/article/28013377080/.

On the other hand, sometimes I can include a Windows path in a comment, without triggering a unicode error. I don't understand why some Windows paths trigger errors, and other paths do not.

The following are a few examples of Windows paths that do, or do not cause encoding errors, as indicated below:

'''

OK      # E:\Apps\ParticlesByMarc\regularexpression_info_SAVE_aaa_.py
ERROR   # E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py
OK      # E:\Apps\ UnitiesByMarc\regularexpression_info_SAVE_aaa_.py# File 
ERROR   # E:\ Apps\ UnitiesByMarc\xxx\regularexpression_info_SAVE_aaa_py
OK      # E:\ Apps\ UnitiesByMarc\ xxx\regularexpression_info_SAVE_aaa_py
OK      # File E:\ Apps\ UnitiesByMarc\x123x\regularexpression_info_SAVE_aaa_py

'''

I cannot figure out what makes two of those Windows path formats OK to be included in a comment, and the other four not OK to be included in a comment.

My questions:

  1. Is there something I could do to format the comment so that I would not have to insert a space after each backslash?
  2. If there are other limits on text that can be included in a comment, where can I find a list of those limits?
  3. Where can I find the rules that identify and explain the reason for the limitations?

Any suggestions about how to find the answer would be very welcome.

Thanks, Marc

Marc B. Hankin
  • 771
  • 3
  • 15
  • 31
  • 1
    Read about special meaning of [Escape Sequences](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals) `\u`, `\U`, `\x` etc… – JosefZ Jul 28 '21 at 18:12
  • Thank you so much for the reference. Because I have not learned any C yet, I do not understand some of it, perhaps most of it. But I get the impression that the best work around is for me to replace Windows backslashes with forward slashes when I put Windows paths in comments. Beyond that, it's a little over my head. But now I know that it's something I have to learn. So, thank you. – Marc B. Hankin Jul 29 '21 at 16:15
  • 1
    You can use a _raw string_ (explained in the same link) `r'''E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py'''`. – JosefZ Jul 29 '21 at 18:49
  • Thank you again. That's a great solution, but my goal is to include Windows paths in multiline triple quoted comments. I get an error message if, for example, I make a comment like this one: """ Hello [new line character] r'''E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py''' [new line character] """ – Marc B. Hankin Jul 30 '21 at 19:37

1 Answers1

1

A triple quoted string isn't a comment; it's a string which could become a Docstring:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

Example:

def somefunc(somepar):
  r'''
This is a docstring

  E:\Apps\ParticlesByMarc\regularexpression_info_SAVE_aaa_.py
  E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py
# E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py # File 
# E:\Apps\UnitiesByMarc\xxx\regularexpression_info_SAVE_aaa_py
  E:\Apps\UnitiesByMarc\xxx\regularexpression_info_SAVE_aaa_py
# File E:\Apps\UnitiesByMarc\x123x\regularexpression_info_SAVE_aaa_py

  '''
  print('supplied:', somepar, end='\n\n')
  '''
This isn't recognized as a docstring (i.e. not assigned to __doc__)
  '''


somefunc('par')
help(somefunc)

Result: .\SO\68553726.py

supplied: par

Help on function somefunc in module __main__:

somefunc(somepar)
    This is a docstring

      E:\Apps\ParticlesByMarc\regularexpression_info_SAVE_aaa_.py
      E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py
    # E:\Apps\UnitiesByMarc\regularexpression_info_SAVE_aaa_.py # File
    # E:\Apps\UnitiesByMarc\xxx\regularexpression_info_SAVE_aaa_py
      E:\Apps\UnitiesByMarc\xxx\regularexpression_info_SAVE_aaa_py
    # File E:\Apps\UnitiesByMarc\x123x\regularexpression_info_SAVE_aaa_py
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Dear JosefZ, I can't thank you enough. I have spent days trying to figure out what the problem was, and I obviously did not understand solutions that probably should have been obvious. Thank you for your kindness in showing me the solution. – Marc B. Hankin Aug 01 '21 at 00:50
  • Dear JosefZ, I need to really study the Docstring material you kindly brought to my attention. But I couldn't resist ---> fyi, See Guido's tweet at https://twitter.com/gvanrossum/status/112670605505077248?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E112670605505077248%7Ctwgr%5E%7Ctwcon%5Es1_&ref_url=https%3A%2F%2Fsgillies.net%2F2017%2F05%2F30%2Fpython-multi-line-comments-and-triple-quoted-strings.html – Marc B. Hankin Aug 02 '21 at 01:50
  • @MarcB.Hankin sorry I'm not Twitter (and alike) user… – JosefZ Aug 02 '21 at 12:14
  • Nor am I. There's no reason for you to apologize. You are wonderful. The help you gave me was invaluable and I've been trying to figure this out for years. I can't thank you enough. The spunky aspect of my character impelled me to bring Guido's statement to your attention because I thought you'd to know about it. I wish I could reciprocate for all the help you gave me. Marc – Marc B. Hankin Aug 04 '21 at 17:11