0

I'm reading Exercise 16 of "Learn Python The Hard way" and I'm confused about open(filename, "w") -- what does the "w" mean?

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
chandler11able
  • 123
  • 1
  • 1
  • 7

4 Answers4

4

w stands for writing permission for the opened file

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

Looking at the signature we can (often) understand what each argument does more info here

raam86
  • 6,785
  • 2
  • 31
  • 46
1

The second argument in opening a filename represents the mode you're using (i.e., read-only, writable). In this case, it's to be able to (w)rite to the file.

https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

thumbtackthief
  • 6,093
  • 10
  • 41
  • 87
1

The "W" means that you are opening the file called filename with the purpose of writing to it(hence the "W" for write.)

Benjamin D
  • 11
  • 1
1

The "w" specifies that the file is being written to. As you can see here, "w" is the mode that you use when writing to a file.

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists)...

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76