1

So I am on that chapter and another post showed me how to reduce the code to a much more condensed 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 cant I use the same format that open(to_file,'w') uses, for the part that says: open(from_file).read()

Can I use something similar like open(from_file, 'r') in that part of the code? Why or why not?

7
  • are you asking what is a difference between open(something, 'r') and open(something, "w")? Your problem description is very unclear. Commented Jul 6, 2016 at 21:45
  • Are you getting an error message? This worked fine for me. open(to_file, 'w').write(open(from_file, 'r').read()) Note, I did include 'r' in the file being read from. Commented Jul 6, 2016 at 21:49
  • @Will, I meant take out the ".read" part entirely and replace it with 'r' next to the open(from_file) like this: open(from_file, 'r'). I guess a better question is why is the .read part even necessary if its already a default option under open(from_file)? If it is necessary at all. Commented Jul 7, 2016 at 17:03
  • Ah, well you need read() because open(filename) returns a file handle object, not a string. In order to get the contents of a file as a string, you must call read() on the file object. Commented Jul 7, 2016 at 17:51
  • @Will, thank you again for your help in understanding this. One last question if it's ok, why is closing the file not necessary? I am assuming, obviously incorrectly, that since we opened the file to read the contents and then replace them, that it needs to be closed, but apparently its not necessary, why is that? What am I not seeing? Thanks again. Commented Jul 29, 2016 at 4:33

2 Answers 2

0

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

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

[...] The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'.

So you can use the read-only flag, but not obliged to do so, i.e. open(file, 'r') is equivalent to open(file)

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your time. So, why is the .read part necessary in this part of the code? Is asking to read the from_file again?: open(from_file, 'r').read()))
@ Xander I think I get it, so basically, the open() function tells the compiler to switch to a mode 'r', w, or a. THEN, the .read part of the code is the actual execution of the mode?
@dubg there is no compilation un python, but an interpreter that read the code at execution. The open() function just gets a cursor to parse the file. The read() uses this cursor to actually read the content of the file.
0

You can.

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

Is valid and should work the same.

The access modes in open() are optional, as stated in documentation. When not specified it will add the "r" for you. Quoted below.

access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

To break this line down further, the open() function basically opens the file up and returns a file object type. Then you take that returned file type and immediately call its write() function. This function expects a string, which you provide with open(from_file, "r").read(). This reads the entire file and returns a string. Thus in the end, it opens a file and writes the contents of another.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.