Python 使用tempfile包輕松無痕的運行代碼
一、簡介
這里介紹python中臨時文件及文件夾使用。
使用的是tempfile包:
pip install tempfile
https://docs.python.org/3/library/tempfile.html
二、臨時文件夾
2.1 獲取臨時文件夾
# 獲取臨時文件夾 tmpdir = tempfile.gettempdir() print(tmpdir) #/tmp
2.2 生成臨時文件夾
# 方式一:生成默認(rèn)臨時文件夾 tmpdir = tempfile.mkdtemp() print(tmpdir) #/tmp/tmpui77cgud # 方式二:生成自定義臨時文件夾(指定前綴、后綴、目錄,可指定其中一部分),suffix:后綴, prefix:前綴, dir:目錄 tmpdir = tempfile.mkdtemp(suffix='_txt', prefix='tp_dir_', dir='/home/tmp/py_rs_file') print(tmpdir) # /home/tmp/py_rs_file/tp_dir_06l_o2dm_txt
三、臨時文件
3.1 生成不自動刪除(關(guān)閉時)的臨時文件
# 方式一:生成默認(rèn)臨時文件,默認(rèn)為二進(jìn)制文件 tmpfile = tempfile.mkstemp()[1] print(tempfile) #/tmp/tmp75kazf_8 # 數(shù)據(jù)寫入 with open(tmpfile, 'w+') as t_f: t_f.writelines('hello world') # 方式二:生成自定義臨時文件(指定前綴、后綴、目錄、文件類型參數(shù),可指定其中一部分),suffix:后綴, prefix:前綴, dir:目錄, text:文件類型,True為文本,false為二進(jìn)制 tmpfile = tempfile.mkstemp(suffix='.txt', prefix='tp_', dir='/home/tmp/py_rs_file', text=True)[1] print(tempfile) # /home/tmp/py_rs_file/tp_pn2973g0.txt # 數(shù)據(jù)寫入 with open(tmpfile, 'w+') as t_f: t_f.writelines('hello world')
3.2 生成自動刪除的臨時文件
# 方式一:創(chuàng)建臨時文件,文件關(guān)閉時自動刪除 tmpfile = tempfile.TemporaryFile(mode='w+t') tmpfile.write('hello world') ##數(shù)據(jù)寫入 tmpfile.seek(0) tmpTxt = tmpfile.read() #數(shù)據(jù)讀取 print(tmpTxt) tmpfile.close() #關(guān)閉時文件自動刪除 # 方式二:創(chuàng)建臨時文件,文件關(guān)閉時根據(jù)delete參數(shù)確定是否自動刪除, True:刪除 False:不刪除 with tempfile.NamedTemporaryFile(delete=False) as tmpfile: file_name = tmpfile.name print(file_name) #/tmp/tmp73zl8gmn tmpfile.write('hello world'.encode()) tmpfile.seek(0) tmpTxt = tmpfile.read().decode() print(tmpTxt) # 方式三:創(chuàng)建自定義臨時文件,文件關(guān)閉時可根據(jù)delete參數(shù)確定是否自動刪除, True:刪除 False:不刪除 # 其他配置參數(shù)有,mode:文件模式(w+b為二進(jìn)制模式(默認(rèn)),w+t為文本模式),suffix:后綴, prefix:前綴, dir:目錄 with tempfile.NamedTemporaryFile(mode='w+t', suffix='.txt', prefix='tp_', dir='/home/tmp/py_rs_file',delete=False) as tmpfile: file_name = tmpfile.name print(file_name) #/home/tmp/py_rs_file/tp_fcwpmh3l.txt tmpfile.write('hello world') tmpfile.seek(0) tmpTxt = tmpfile.read() print(tmpTxt)
根據(jù)具體情況,臨時資源可以直接調(diào)用內(nèi)存或數(shù)據(jù)庫存儲。
技術(shù)交流
歡迎轉(zhuǎn)載、收藏、有所收獲點贊支持一下!
到此這篇關(guān)于Python 使用tempfile包輕松無痕的運行代碼的文章就介紹到這了,更多相關(guān)Python tempfile包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之列表與元組詳解
序列是Python中最基本的數(shù)據(jù)結(jié)構(gòu)。序列中的每個元素都分配一個數(shù)字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推,元組與列表類似,不同之處在于元組的元素不能修改。元組使用小括號,列表使用方括號2021-10-10Python實現(xiàn)網(wǎng)絡(luò)自動化eNSP
這篇文章主要介紹了Python實現(xiàn)網(wǎng)絡(luò)自動化eNSP,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Python爬蟲基礎(chǔ)之selenium庫的用法總結(jié)
今天帶大家來學(xué)習(xí)selenium庫的使用方法及相關(guān)知識總結(jié),文中非常詳細(xì)的介紹了selenium庫,對正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下2021-05-05詳解OpenCV自適應(yīng)直方圖均衡化的應(yīng)用
在本文中,將介紹如何應(yīng)用對比度受限的自適應(yīng)直方圖均衡化 ( Contrast Limited Adaptive Histogram Equalization, CLAHE ) 來均衡圖像,需要的可以參考一下2022-02-02tensorflow實現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集)
這篇文章主要介紹了tensorflow實現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python中循環(huán)后使用list.append()數(shù)據(jù)被覆蓋問題的解決
這篇文章主要給大家介紹了關(guān)于Python中循環(huán)后使用list.append()數(shù)據(jù)被覆蓋問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07