import os fd = os.open('custom.txt', os.O_WRONLY | os.O_CREAT | os.O_TRUNC) with open(fd, 'w') as f: f.write('via fd') os.close(fd) with open('custom.txt') as f: print(repr(f.read()))