Python script accessing own source code

Robin Becker robin at reportlab.com
Thu May 13 03:25:41 EDT 2021


On 12/05/2021 20:17, Mirko via Python-list wrote:
> Am 12.05.2021 um 20:41 schrieb Robin Becker:
>> .......
>>>
>...........
>> since GvR has been shown to have time traveling abilities such a
>> script could paradoxically appear acausally.
>> -- 
>> yrs-not-too-seriously
>> Robin Becker
> 
> 
> Not sure, if that's what you mean, but writing a self-replicating
> script is easy too:

actually I was really joking about self creating scripts that do something useful like finding future lotto numbers.

the artificial programmer servant is some way off

> 
> import os
> import sys
> 
> with open(os.path.abspath(__file__)) as myself:
>      with open(sys.argv[1], "w") as yourself:
>          yourself.write(myself.read())
> 
> 
> Give it a filename as a command-line argument and it will write
> itself to that file.
> 
--
Robin Becker



More information about the Python-list mailing list