Learning Python, the hard way, out of 17, I got one liner, but

So, I’m on this chapter and another post showed me how to reduce the code to a more compressed version

from sys import argv

from os.path import exists

script, from_file, to_file = argv

(open(to_file, 'w').write(open(from_file).read()))

My question on line 6 is why I am not using the same format that I use open(to_file,'w')for the part that says:open(from_file).read()

Is it possible to use something similar, for example, open(from_file, 'r')in this part of the code? Why or why not?

+1
source share
2 answers

If you refer to the documentation open(), the open mode is optional and is read-only by default: https://docs.python.org/2/library/functions.html#open

open (name [, mode [, buffering]])

[...] : "r" , "w" ( , ), "a" ( Unix , ). , "r".

, , , .. open(file, 'r') open(file)

0

.

(open(to_file, 'w').write(open(from_file, "r").read()))

.

open() , . , "r". .

access_mode: access_mode , , .. , , .. . , (r).

, open() file. write(). , open(from_file, "r").read(). . , .

0

Source: https://habr.com/ru/post/1649543/


All Articles