Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing overwriting when using numpy.savetxt

Tags:

numpy

Is there built error handling for prevent overwriting a file when using numpy.savetxt? If 'my_file' already exists, and I run

numpy.savetxt("my_file", my_array)

I want an error to be generated telling me the file already exists, or asking if the user is sure they want to write to the file.

like image 536
astromonerd Avatar asked Dec 26 '22 13:12

astromonerd


1 Answers

You can check if the file already exists before you write your data:

import os

if not os.path.exists('my_file'): numpy.savetxt('my_file', my_array)
like image 148
some_weired_user Avatar answered Dec 28 '22 15:12

some_weired_user