开发者社区> 问答> 正文

python中的temp是什么文件

python中的temp是什么文件

展开
收起
云计算小粉 2018-05-10 20:10:38 2424 0
1 条回答
写回答
取消 提交回答
  • ==tempfile 模块==

    [Example 2-6 #eg-2-6] 中展示的 tempfile 模块允许你快速地创建名称唯一的临时文件供使用.

    ====Example 2-6. 使用 tempfile 模块创建临时文件====[eg-2-6]

    File: tempfile-example-1.py
    
    import tempfile
    import os
    
    tempfile = tempfile.mktemp()
    
    print "tempfile", "=>", tempfile
    
    file = open(tempfile, "w+b")
    file.write("*" * 1000)
    file.seek(0)
    print len(file.read()), "bytes"
    file.close()
    
    try:
        # must remove file when done
        os.remove(tempfile)
    except OSError:
        pass
    
    tempfile => C:\TEMP\~160-1
    bytes

    TemporaryFile 函数会自动挑选合适的文件名, 并打开文件, 如 [Example 2-7 #eg-2-7] 所示.
    而且它会确保该文件在关闭的时候会被删除. (在 Unix 下, 你可以删除一个已打开的文件, 这
    时文件关闭时它会被自动删除. 在其他平台上, 这通过一个特殊的封装类实现.)

    ====Example 2-7. 使用 tempfile 模块打开临时文件====[eg-2-7]

    File: tempfile-example-2.py
    
    import tempfile
    
    file = tempfile.TemporaryFile()
    
    for i in range(100):
        file.write("*" * 100)
    
    file.close() # removes the file!
    2019-07-17 22:21:31
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载