User avatar
Alex_Landherr
Posts: 47
Joined: Mon May 18, 2020 3:14 pm
Location: Sweden
Contact: Twitter

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

Sat Jun 20, 2020 8:27 pm

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?
Helpful answers are appreciated. I usually respond to Tweets directed at me. JWST is the Sagrada Família of spacecrafts. I code in Python on Windows 10 and Raspberry Pi OS.

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

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

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.

User avatar
RaspbianUser1
Posts: 495
Joined: Thu Mar 05, 2020 6:34 pm

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

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
Running either Raspberry Pi OS desktop Full or Raspberry Pi OS MATE desktop
Overclocked at 2200MHz CPU and 700 MHz GPU with a over voltage of 7
Think before you delete something a stranger on the internet told you to.

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

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

Sun Jun 21, 2020 3:25 am

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!

User avatar
Paeryn
Posts: 3079
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

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

Sun Jun 21, 2020 3:26 am

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).
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

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

Sun Jun 21, 2020 7:19 am

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

User avatar
Cancelor
Posts: 778
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

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

Sun Jun 21, 2020 7:46 am

Just try it and see what happens!
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
RaspbianUser1
Posts: 495
Joined: Thu Mar 05, 2020 6:34 pm

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

Sun Jun 21, 2020 11:44 am

Welp, I’ve been doing the open command wrong...
Running either Raspberry Pi OS desktop Full or Raspberry Pi OS MATE desktop
Overclocked at 2200MHz CPU and 700 MHz GPU with a over voltage of 7
Think before you delete something a stranger on the internet told you to.

User avatar
Alex_Landherr
Posts: 47
Joined: Mon May 18, 2020 3:14 pm
Location: Sweden
Contact: Twitter

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

Tue Jun 23, 2020 2:01 pm

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.
Helpful answers are appreciated. I usually respond to Tweets directed at me. JWST is the Sagrada Família of spacecrafts. I code in Python on Windows 10 and Raspberry Pi OS.

Return to “Beginners”