Page 1 of 1

Does Appending to a File That Doesn't Exist Create A File?

Posted: Sat Jun 20, 2020 8:27 pm
by Alex_Landherr
I've been coding from this tutorial: https://projects.raspberrypi.org/en/pro ... rature-log. It has a few minor errors, although irrelevant for this post.
My real question to the experienced programmers of this community is if using the code below actually creates a new file if it does not exist?

Code: Select all

from gpiozero import CPUTemperature
from time import sleep, strftime, time

with open("/media/pi/RPI4B/Videos/Pi_Camera_Videos/cpu_temp.csv", "a") as log:
    while True:
        temp = CPUTemperature()
        log.write("{0}, {1}°C\n".format(strftime("UTC: %Y-%m-%d %H:%M:%S"),str(temp)[-6:-1]))
        sleep(1)
According to my introductory book "Introducing Python - Modern Computing in Simple Packages" by Bill Lubanovic he writes as follows: "a means append (write after the end) if the file exists."

Does this mean that the modified example creates a file (in my case a csv file for logging the CPU temperature) when it "sees" that cpu_temp does not exist? Or am I missing something here?

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sat Jun 20, 2020 8:53 pm
by pfletch101
Yes. Append implies create if the file does not exist. This makes a reasonable amount of sense if you think about it - you are appending to a previously non-existent zero-length file.

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sun Jun 21, 2020 1:05 am
by RaspbianUser1
Sorry if wrong but im pretty sure it doesn’t make a file but replaced with “a+” instead it does

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sun Jun 21, 2020 3:25 am
by pfletch101
RaspbianUser1 wrote:
Sun Jun 21, 2020 1:05 am
Sorry if wrong but im pretty sure it doesn’t make a file but replaced with “a+” instead it does
You are correct - I was having a senior moment!

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sun Jun 21, 2020 3:26 am
by Paeryn
RaspbianUser1 wrote:
Sun Jun 21, 2020 1:05 am
Sorry if wrong but im pretty sure it doesn’t make a file but replaced with “a+” instead it does
No, :-
  • "a" means open for writing, if the file exists append to it, if the file doesn't exist create it.
  • "w" means open for writing, if the file exists truncate it first, if the file doesn't exist create it.
  • "x" means open the file for writing only if the file doesn't exist, if the file does exist then return an error.
  • "r" means open the file for reading.
  • adding a "+" to the mode means open for both reading & writing.
  • adding a "b" to the mode means open the file in binary i.e. don't transform data (so new lines aren't converted).

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sun Jun 21, 2020 7:19 am
by B.Goode
RaspbianUser1 wrote:
Sun Jun 21, 2020 1:05 am
Sorry if wrong but im pretty sure it doesn’t make a file but replaced with “a+” instead it does

That's not what the apparently 'official' Python3 documentation says -
The second argument is another string containing a few characters describing the way in which the file will be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.
Ref: https://docs.python.org/3/tutorial/inpu ... ting-files

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sun Jun 21, 2020 7:46 am
by Cancelor
Just try it and see what happens!

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Sun Jun 21, 2020 11:44 am
by RaspbianUser1
Welp, I’ve been doing the open command wrong...

Re: Does Appending to a File That Doesn't Exist Create A File?

Posted: Tue Jun 23, 2020 2:01 pm
by Alex_Landherr
pfletch101 wrote:
Sat Jun 20, 2020 8:53 pm
Yes. Append implies create if the file does not exist. This makes a reasonable amount of sense if you think about it - you are appending to a previously non-existent zero-length file.
Thanks, it works.