Python Mod-Script-Pipe Export2 Only Works Once

I’m trying to make my script export to date and time when ran. So far, it exports to the date and time, but only once. Let’s say I export a recording saying, “Hi!” it would export to the date and time. But then I export me saying “Bye!” it would change the file saying hi to the date and time and overwrite the “Hi!” for “Bye!” What can I do to make this not happen? FYI: button is my record and stop recording button (works fine) and button2 is my export button (works fine)
Code:

from gpiozero import Button
from signal import pause
import os
import sys
import datetime

if sys.platform == 'win32':
    print("pipe-test.py, running on windows")
    TONAME = '\\\\.\\pipe\\ToSrvPipe'
    FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
    EOL = '\r\n\0'
else:
    print("pipe-test.py, running on linux or mac")
    TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
    FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
    EOL = '\n'

print("Write to  \"" + TONAME +"\"")
if not os.path.exists(TONAME):
    print(" ..does not exist.  Ensure Audacity is running with mod-script-pipe.")
    sys.exit()

print("Read from \"" + FROMNAME +"\"")
if not os.path.exists(FROMNAME):
    print(" ..does not exist.  Ensure Audacity is running with mod-script-pipe.")
    sys.exit()

print("-- Both pipes exist.  This is a good thing.")

TOFILE = open(TONAME, 'w')
print("-- File to which to write has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File from which to read has now been opened also\r\n")
def send_command(command):
    """Send a single command."""
    print("Send: >>> \n"+command)
    TOFILE.write(command + EOL)
    TOFILE.flush()

date = datetime.datetime.now().strftime('%d-%m-%y-%H:%M:%S')

def record():
        send_command("Record1stChoice")

def stop():
        send_command("Stop") 

def export():
        send_command("SelectAll")
        send_command("Export2: Filename=/home/pi/raspberrypi/Desktop/Audio_Files/{}.mp3".format(date)) 

button = Button(2)
button2 = Button(17)

button.when_pressed = record
button.when_released = stop

button2.when_pressed = export

pause()

You assign a value to date just once, so each time you export will overwrite the previous export.
Try something like:

f"Export2: Filename=/home/.../Audio_Files/{get_date_string()}.mp3"

where get_date_string() calls a function that returns the current time as a string.

I added the

f"Export2: Filename=/home/pi/raspberrypi/Audio_Files/{get_date_string()}.mp3"

And it said get_string_date is undefined.
The only line I changed is the send_command(“Export2”) line. What should I do?

You will need to define the function get_date_string() in your Python code.

Off the top of my head you will need something like:

from datetime import  datetime

...

def get_date_string():
    return datetime.now().strftime('%d-%m-%y-%H:%M:%S')

Thanks, @steve I added the last part you said and it worked!

This topic was automatically closed after 30 days. New replies are no longer allowed.