fileToHTTP.py 文件源码

python
阅读 41 收藏 0 点赞 0 评论 0

项目:OVERWATCH 作者: raymondEhlers 项目源码 文件源码
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")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号