Print out all code in a Python script

前端 未结 1 783
再見小時候
再見小時候 2021-01-26 04:59

How can I print out the contents of a Python script?

For example, if I have this (pointless example) script:

my_var = 1 + 1
another_thing = \"this is my          


        
相关标签:
1条回答
  • 2021-01-26 05:48

    Programs that print out their source code are called quines. There are a few good examples here.

    The easiest way is to read the script's source file:

    print open(__file__).read()
    

    If you can't read from the file, you'll need to wrap all your code in strings. You can then execute the strings and print them separately.

    0 讨论(0)
提交回复
热议问题