Python os.tmpfile() 方法

Python File(文件) 方法 Python OS 文件/目錄方法


概述

os.tmpfile() 方法用于返回一個(gè)打開(kāi)的模式為(w+b)的臨時(shí)文件對(duì)象,這文件對(duì)象沒(méi)有文件夾入口,沒(méi)有文件描述符,將會(huì)自動(dòng)刪除。

語(yǔ)法

tmpfile()方法語(yǔ)法格式如下:

os.tmpfile

參數(shù)

  • 無(wú)

返回值

返回一個(gè)臨時(shí)文件對(duì)象。

實(shí)例

以下實(shí)例演示了 tmpfile() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os

# 創(chuàng)建臨時(shí)文件對(duì)象
tmpfile = os.tmpfile()
tmpfile.write('臨時(shí)文件在這創(chuàng)建了.....')
tmpfile.seek(0)

print tmpfile.read()
tmpfile.close

執(zhí)行以上程序輸出結(jié)果為:

臨時(shí)文件在這創(chuàng)建了.....

Python File(文件) 方法 Python OS 文件/目錄方法