A one-line version of your code could end up looking like this:
exec("""\nfrom sys import argv\nscript,from_file,to_file = argv\nprint(f"copying from {from_file} to {to_file}")\nin_file = open(from_file)\nindata = in_file.read()\ninput("move one == enter")\nout_file = open(to_file,'w')\nout_file.write(indata)\nout_file.close()\nin_filen.close()\n""")
or this:
(lambda __g, __print: (lambda __mod: [[(__print('copying from {from_file} to {to_file}'), [[(input('move one == enter'), [(out_file.write(indata), (out_file.close(), (in_filen.close(), None)[1])[1])[1] for __g['out_file'] in [(open(to_file, 'w'))]][0])[1] for __g['indata'] in [(in_file.read())]][0] for __g['in_file'] in [(open(from_file))]][0])[1] for (__g['script'], __g['from_file'], __g['to_file']) in [(argv)]][0] for __g['argv'] in [(__mod.argv)]][0])(__import__('sys', __g, __g, ('argv',), 0)))(globals(), __import__('__builtin__', level=0).__dict__['print'])
The first way is intuitive. The second way is left as an exercise to figure out what is going on there. There are online tools around the net to convert your multiline Python to a single line.
Simple Tool
Here is a simple technique I use when I need to one-linify and run Python on a remote system.
# Howdy
def main():
print("Hello,")
print("World")
# That was fun!
if __name__ == "__main__" or True:
# BEGIN #
import pipes
import re
with open(__file__) as f:
contents = f.read()
regex = r"^(.+?)#\sBEGIN\s#.+?#\sEND\s#(.+)$"
core = re.sub(regex, "\\1\\2", contents, 0, re.MULTILINE | re.DOTALL)
escaped = f"{core!r}"
outer = pipes.quote(f"exec({escaped})")
oneline = f"python -c {outer}"
print(oneline)
# END #
main()
Output:
python -c 'exec('"'"'# Howdy\ndef main():\n print("Hello,")\n print("World")\n # That was fun!\n\nif __name__ == "__main__" or True:\n\n \n\n main()\n'"'"')'
Hello,
World
The one-line code is thus:
python -c 'exec('"'"'# Howdy\ndef main():\n print("Hello,")\n print("World")\n # That was fun!\n\nif __name__ == "__main__" or True:\n\n \n\n main()\n'"'"')'
Explanation
The tool is spelled out so it is easy to follow, but crucially the code at the bottom reads the entire contents of the current file, removes the tool code from itself, escapes newlines and quotes, then wraps it all in an exec
- very meta!
Oh, and the if __name__ == "__main__" or True:
is just because I like to run my scripts in PyCharm with the helpful play button in the gutter. Feel free to omit this.
The OP's Specific Case
def main():
from sys import argv
_, script, from_file, to_file = argv
print(f"copying from {from_file} to {to_file}")
in_file = open(from_file)
indata = in_file.read()
input("move one == enter")
out_file = open(to_file,'w')
out_file.write(indata)
out_file.close()
in_file.close()
if __name__ == "__main__" or True:
# BEGIN #
import pipes
import re
with open(__file__) as f:
contents = f.read()
regex = r"^(.+?)#\sBEGIN\s#.+?#\sEND\s#(.+)$"
core = re.sub(regex, "\\1\\2", contents, 0, re.MULTILINE | re.DOTALL)
escaped = f"{core!r}"
outer = pipes.quote(f"exec({escaped})")
oneline = f"python3 -c {outer}"
print(oneline)
# END #
main()
Running the above (and note the argv
shift I made to account for the -c
), here is your one-liner:
python3 -c 'exec('"'"'def main():\n from sys import argv\n _, script, from_file, to_file = argv\n print(f"copying from {from_file} to {to_file}")\n in_file = open(from_file)\n indata = in_file.read()\n input("move one == enter")\n out_file = open(to_file,\'"'"'w\'"'"')\n out_file.write(indata)\n out_file.close()\n in_file.close()\n\nif __name__ == "__main__" or True:\n\n \n\n main()\n'"'"')'
You then run the above and add your three arguments to the end of that command as if it were a file. HTH.