Copy-paste into Python interactive interpreter and indentation

前端 未结 8 1687
借酒劲吻你
借酒劲吻你 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 01:02

    All of the current answers suggest you change to IPython. For a Python-only solution, you can use textwrap to remove leading whitespace from lines.

    For example,

    >>> code="""    x='your pasted code'
                    y='with common indentation'"""
    >>> formatted=textwrap.dedent(code)
    >>> exec(formatted)
    

提交回复
热议问题