open() in Python does not create a file if it doesn’t exist September 7, 2022 by Tarik You should use open with the w+ mode: file = open('myfile.dat', 'w+')