最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Python中如何判断变量类型是否是打开的文件对象

Python crifan 65浏览 0评论
在优化和整理函数resizeImage,已经通过:
    openableImage = None
    if isinstance(inputImage, str):
        openableImage = inputImage
    elif isinstance(inputImage, bytes):
        print("len(inputImage)=%s" % len(inputImage))
        openableImage = io.BytesIO(inputImage)
支持了是filename还是binary bytes,现在继续要支持判断输入的变量是否是的open的file的object
用代码:
imageFilename = "/Users/crifan/dev/tmp/python/resize_image_demo/hot day.png"
imageFileObj = open(imageFilename, "rb")
outputImageFilename = "/Users/crifan/dev/tmp/python/resize_image_demo/hot day_600x600.png"
beforeTime = datetime.datetime.now()
resizeImage(imageFileObj, (600, 600), outputImageFile=outputImageFilename)
调试期间发现是:
好像是:
<_io.BufferedReader name=’/Users/crifan/dev/tmp/python/resize_image_demo/hot day.png’>
类型的
python file object
2.3.9 File Objects
File Objects — Python 3.7.2rc1 documentation
python check  file object type
Check if object is file-like in Python – Stack Overflow
Check if an object is a file in Python 2 and 3 – Stack Overflow
Python 3.3.2 check that object is of type file – Stack Overflow
python – How do you check if an object is an instance of ‘file’? – Stack Overflow
io — Core tools for working with streams — Python 3.7.2rc1 documentation
Python3中open出来的file是
Glossary — Python 3.7.2rc1 documentation
“file object
An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.
There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function.
file-like object
A synonym for file object.”
“binary file
A file object able to read and write bytes-like objects. Examples of binary files are files opened in binary mode (‘rb’, ‘wb’ or ‘rb+’), sys.stdin.buffer, sys.stdout.buffer, and instances of io.BytesIO and gzip.GzipFile.
See also text file for a file object able to read and write str objects.
bytes-like object
An object that supports the Buffer Protocol and can export a C-contiguous buffer. This includes all bytes, bytearray, and array.array objects, as well as many common memoryview objects. Bytes-like objects can be used for various operations that work with binary data; these include compression, saving to a binary file, and sending over a socket.
Some operations need the binary data to be mutable. The documentation often refers to these as “read-write bytes-like objects”. Example mutable buffer objects include bytearray and amemoryview of a bytearray. Other operations require the binary data to be stored in immutable objects (“read-only bytes-like objects”); examples of these include bytes and a memoryview of a bytes object.”
【总结】
目前用:

def isFileObject(fileObj):
    """"check is file like object or not"""
    if sys.version_info[0] == 2:
        return isinstance(fileObj, file)
    else:
        # for python 3:
        # has read() method for:
        # io.IOBase
        # io.BytesIO
        # io.StringIO
        # io.RawIOBase
        return hasattr(fileObj, 'read')
可以判断出open后对象是file:
imageFileObj = open(imageFilename, "rb")
outputImageFilename = "/Users/crifan/dev/tmp/python/resize_image_demo/hot day_600x600.png"
resizeImage(imageFileObj, (600, 600), outputImageFile=outputImageFilename)

def resizeImage(inputImage,
。。。
    elif isFileObject(inputImage):
      openableImage = inputImage

转载请注明:在路上 » 【已解决】Python中如何判断变量类型是否是打开的文件对象

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
18 queries in 0.089 seconds, using 9.32MB memory