-7

I want to put the following code in one single line:

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_filen.close()

Is this possible?

I think it was with ;, but I don't remember.

Christian König
  • 3,437
  • 16
  • 28
AhZ
  • 19
  • 1
  • 2
  • 1
    `print("What have you tried so far?"); print("What specific errors did you encounter?")` – Christian König Nov 06 '18 at 13:51
  • 1
    What is exactly your problem? I don't understand your question... – toti08 Nov 06 '18 at 13:51
  • Well you could concatenate multiple statements in one line with semicolons but why would you want to make code terribly ugly? – meissner_ Nov 06 '18 at 13:54
  • use `shutil.copyfile()` – Chris_Rands Nov 06 '18 at 13:56
  • 1
    But in general reducing the number of lines of code is not by itself a useful aim in python, if you want to stick with your current structure then use the `with` statement to handle opening and closing of files – Chris_Rands Nov 06 '18 at 13:57
  • i just want to learn the basics of python so im trying to learn what i can + i did use semicolon but my code didn't work so... i thought that maybe im doing it wrong – AhZ Nov 06 '18 at 13:58
  • 1
    Hey. I see this is your first post here :) Perhaps you can review [the information on asking](https://stackoverflow.com/help/asking) and come back to improve your question. – Carlos Mermingas Nov 06 '18 at 13:59

3 Answers3

1

Read the docs!

A simple statement is comprised within a single logical line. Several simple statements may occur on a single line separated by semicolons.

Search Stack Overflow! How to put multiple statements in one line?

Or google, to find converters for more complex programs: https://onelinepy.herokuapp.com/

Christian König
  • 3,437
  • 16
  • 28
0

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.

Drakes
  • 23,254
  • 3
  • 51
  • 94
0

Hey you can use this tool https://jagt.github.io/python-single-line-convert/

It converts the code into single line. Then you can execute to console for directly int python REPL