That's not resistant against name collisions if you run multiple tasks at the same time. Better to use whatever file system functions your OS provides to create uniquely named files.
At some point in your career, you'll just copy form code from stack overflow, a random forum, or AI to put a timestamp the on file created that outputs all the way to the exact microsecond the file was created. You're pat yourself on the back for having fixed the problem of accidently over writing your old file.
But then six months will go back and you'll realize that you can't read the date and time output on the file. Worse, you might realize it doesn't make sense because your coworker changed it to a unix timestamp which requires being converted to read correctly (which is a huge pain in the ass to type out). And for all intents and purposes, is worthless now.
That's why you take the already opened file as argument, or better yet if you can get away with it you write unconditionally to stdout and let the user use shell redirections if they want.
If you ever wrote a tool which deletes some files in some folder and didn't accidentally delete the tool itself on the first run, did you even work in this industry.
You see, this is what happens when you stop using good operating systems. DEC's VMS would have just created a new file with an automatic version number, as in FOO.BAR;1 and FOO.BAR;2
Do not open the file in the same function that does calculations. If you must, pass a file descriptor / handle. Or pass a callback function to do the output.
162
u/dminus Oct 01 '26
outfile="$0-$(date +%s).csv"
47
u/honourable_bot Oct 01 '26
Use >> over >
22
u/Cheezyrock Oct 01 '26
Bonus - it takes an 5 hours to run, you notice at 4 hours, panic, and hit stop.
21
u/ICBanMI Oct 01 '26
At some point in your career, you'll just copy form code from stack overflow, a random forum, or AI to put a timestamp the on file created that outputs all the way to the exact microsecond the file was created. You're pat yourself on the back for having fixed the problem of accidently over writing your old file.
But then six months will go back and you'll realize that you can't read the date and time output on the file. Worse, you might realize it doesn't make sense because your coworker changed it to a unix timestamp which requires being converted to read correctly (which is a huge pain in the ass to type out). And for all intents and purposes, is worthless now.
8
u/mr_nonameuser Oct 01 '26
Append?
10
u/theFirstHaruspex Oct 01 '26
output.csv
output_1.csv
output_1.2.csv
output_1butdifferentthistime.csv
5
u/yjlom Oct 01 '26
That's why you take the already opened file as argument, or better yet if you can get away with it you write unconditionally to stdout and let the user use shell redirections if they want.
5
u/dkarlovi Oct 02 '26
If you ever wrote a tool which deletes some files in some folder and didn't accidentally delete the tool itself on the first run, did you even work in this industry.
3
u/DogmaticParadigm87 Oct 02 '26
Ran the export again with mode "w" instead of "a". Previous week: gone. Current week: one lonely row titled final_FINAL2.xlsx.
Related: the backup is named backup, the real file is named backup_old, and the one with yesterday's numbers is in Downloads as untitled(3).
2
u/Usual_Ice636 Oct 01 '26
Thats why you manually rename the file it creates every time.
2
u/razor_train Oct 01 '26
processdata input-data.2026-10-01-ORIGINAL (2) KEEPBACKUPALT.dat2
u/djdaedalus42 Oct 02 '26
You see, this is what happens when you stop using good operating systems. DEC's VMS would have just created a new file with an automatic version number, as in FOO.BAR;1 and FOO.BAR;2
Newer is not always better.
1
u/SeriousPlankton2000 Oct 01 '26
Do not open the file in the same function that does calculations. If you must, pass a file descriptor / handle. Or pass a callback function to do the output.
1
u/russianrug Oct 02 '26
Ahh yes, a tale as old as time…
1
u/LegitimatePants Oct 02 '26
out.txt never dies, it is simply reborn
1
u/ZunoJ Oct 02 '26
New semester?
1
u/BeMyBrutus Oct 02 '26
Indempotent, indemposhant
1
u/perringaiden Oct 07 '26
Who doesn't prefix results files by the
yyyyMMdd_hhmmss_1
u/geekosas Oct 01 '26
Only on vibe code....