Copy-paste into Python interactive interpreter and indentation

前端 未结 8 1685
借酒劲吻你
借酒劲吻你 2020-12-06 00:41

This piece of code, file test.py,

if 1:
   print \"foo\"
print \"bar\"

can be successfully executed with execfile(\"test.py\

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 00:58

    I don't know any trick for the standard command prompt, but I can suggest you a more advanced interpreter like IPython that has a special syntax for multi-line paste:

    In [1]: %cpaste
    Pasting code; enter '--' alone on the line to stop.
    :for c in range(3):
    :    print c
    :
    :--
    0
    1
    2
    

    Another option is the bpython interpreter that has an automatic paste mode (if you are typing too fast to be an human):

    >>> for c in range(3):
    ...     print c
    ... 
    0
    1
    2
    >>> 
      Rewind   Save   Pastebin   Pager   Show Source 
    

提交回复
热议问题