+ 1

How can i make a new file in the computer in case of the first use of the program?

How to make a new file

20th Jul 2019, 3:24 AM
Rafael Marinho dos Anjos
Rafael Marinho dos Anjos - avatar
2 ответов
+ 5
open("my_file", w+) The + sign means that a new file with the name "my_file" will be created if one doesn't already exist. The w means it will be writeable.
20th Jul 2019, 4:22 AM
David Ashton
David Ashton - avatar
+ 1
from what I remember, "w" is for creating an empty file, if it exists it will erase its content. You can't read from a file opened that way, only write. "w+" allows you to also read (what you've written, since previous content of file is erased). 'a' is for appending, if the file doesn't exist, it will create it, sounds nice but each write is automatically appended at the end, you can't write at random in the file. Again, you can't read from a file opened that way, you have to use "a+" to also be able to read (you can read at random, only writes are moved forcefully at the end). 'r' is for reading, the file must exist. You can't write to a file opened that way, you would have to use "r+" to be able to write. So the '+' is to allow 'w' and 'a' to also read, and to 'r' to also write. I hated how none of them did by itself what I wanted: to open a file if it exists, or create it if it doesn't. Because with 'r' the file had to exist, with 'w' it would empty it, and 'a' would only let you write at the end.
20th Jul 2019, 9:41 AM
lion
lion - avatar