lkpat
Executive Member
*heading should read to local file
I have a routine that looks for a file and if it doesn't exist, it creates it, but for some reason, when reading the contents of the file, it's apparently empty. What am I doing wrong?
This basically checks to see if a file (last.txt) exists, and if it doesn't, it creates it and writes a string to it.
When the code is run the first time around (when last.txt does not yet exist), the print (mytime) statement returns an empty string.
When it's run again (last.txt was created the first time it was run and already exists), it's able to read the string that was written to the file.
I have a routine that looks for a file and if it doesn't exist, it creates it, but for some reason, when reading the contents of the file, it's apparently empty. What am I doing wrong?
Code:
import os
if not os.path.isfile('last.txt'):
outfile = open('last.txt', 'w')
print ('2023-01-01 00:00:00', file=outfile)
outfile.close
infile = open('last.txt', 'r')
mytime = infile.readline()
print (mytime)
This basically checks to see if a file (last.txt) exists, and if it doesn't, it creates it and writes a string to it.
When the code is run the first time around (when last.txt does not yet exist), the print (mytime) statement returns an empty string.
When it's run again (last.txt was created the first time it was run and already exists), it's able to read the string that was written to the file.

