def FileWithLocalFilename(filename, writeFile = False):
with tempfile.NamedTemporaryFile() as f:
try:
with FileInMemory(filename) as (success, status, fileInMemory):
if success:
print("Writing to temporary file")
print("success: {}, status: {}".format(success, status))
f.write(fileInMemory.read())
f.flush()
#f.write("Hello")
# Return to start of file so the read is seamless
f.seek(0)
# May be required to fully flush, although flush() seems sufficient for now
# See: https://docs.python.org/2/library/os.html#os.fsync
#os.fsync(f.fileno())
#print("f.read(): {}".format(f.read()))
#f.seek(0)
yield f.name
#print("Post yield")
#f.seek(0, os.SEEK_END)
#print("f length in with def: {}".format(f.tell()))
else:
#yield (False, status, fileInMemory)
yield False
print("Successfully completed FileWithLocalFilename")
except IOError as e:
# Just need an exception so that else is valid.
print("IOError: {}".format(e))
else:
# Only do this if there are no exceptions above
print("Potentially writing file")
if writeFile:
(success, status, returnValue) = putFile(filename = filename, file = f)
print("Wrote file. success: {}, status: {}, returnValue: {}".format(success, status, returnValue))
finally:
print("Finally exiting from FileWithLocalFilename")
评论列表
文章目录