Python SyntaxError: EOF while scanning triple-quoted string literal

29,076

Reason: Python thinks that within your string the file ends.

Guessing: For some reason character 26 is EOF in some cases.

Motivation:
Python files are text files whereas zip files are binary. You should not mix them because

  • the text files have an encoding whereas binary files do not.
  • in Windows, Mac and Linux line endings are different. Text files may be changed accordingly.

In both cases zip binary stuff will break.

Solution:
Encoding.

>>> import base64
>>> base64.b64encode(b"""raw string""") # here you get the encoded result
b'cmF3IHN0cmluZw==' 
>>> base64.b64decode(b'cmF3IHN0cmluZw==') # this is part of your Python file.
b'raw string'
Share:
29,076
Richard Paul Astley
Author by

Richard Paul Astley

Updated on July 16, 2020

Comments

  • Richard Paul Astley
    Richard Paul Astley almost 4 years

    My script is:

    # -*- coding: utf-8 -*-
    RAW_ZIP = """PK    šîF4“àµÑ$=   tcp_host.exeì]
    |GßË]È%\Èa/íµMኦ%ElÀBê…|bÜåÈ”#ƒMIzÙ£¡†<¢\·±¨UQ«©ZµjU´ÑÖh©´”ZªhQ—ÛôB¡œÿ7³{ùÀ¶~ýüéÁìì̼yó%ô6“›¸ŸIÖÃ%,…N–*§xŒ4xF*ÓΞŒÎd.•8¹l¹ j'4ŸZëí‚R;»‰MÊiÈL`
    ...long string...
    "Ýä­+\è\n$NKƒ—u-Èp‰f(OY3ò ˆh&‚"¾ ôE\>Ó]lÀY˜ *¸|ÐZV=Èø4«›„׋³\1òNDØø†R¼pžH5ÇHeòÓêxtŠ‹‰Yí2tªÖE˜"&-')r¢Wå¯AÏk"Õhv%r³\ã&·ù$šR¹ª6ñÕ«›ûP¨6³ÍÍý§ŽÚˆœÛ¢|Øâbý63>8£zŠn`–DÞøUâV“cO§E©¸z½õ—OùÛª|ä‹P‘[¾†ä9ÝGrŠüšK(EöŒíj»<£>M|ù^–¿¦Pß8¯Òw‘é’*3ŸÖh†¬®˜‹µ[]T°³CxÝ­»âUŸ³Ê"RzY,ûŽ—o È#H¶®’ˆ“
    >ÑaËm1èØ÷‰ )ô§ìKvÐ
    c"""
    

    But when i run it i get the error:

    SyntaxError: EOF while scanning triple-quoted string literal
    

    Why?