merge few lines in to one line (python)

1919 views python
3

i want to make one single line code: like we have

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()

and i want to make this in one line is it possible ? i think it was with ; <== this but i dont remember

true noob here dont hate


answered question

print("What have you tried so far?"); print("What specific errors did you encounter?")

What is exactly your problem? I don't understand your question...

Well you could concatenate multiple statements in one line with semicolons but why would you want to make code terribly ugly?

use shutil.copyfile()

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

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

Hey. I see this is your first post here :) Perhaps you can review the information on asking and come back to improve your question.

1 Answer

6

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/

posted this

Have an answer?

JD

Please login first before posting an answer.