久久亚洲精品国产精品_羞羞漫画在线版免费阅读网页漫画_国产精品久久久久久久久久久久_午夜dj免费观看在线视频_希崎杰西卡番号

臨時文件夾(臨時文件夾無寫入權(quán)限,不能安裝)

前沿拓展:

臨時文件夾

微軟的WindowsXP一共有3個臨時文件夾

1.系統(tǒng)臨時文件夾

C盤,Windows\Temp

2.計(jì)算機(jī)用戶帳戶臨時文件夾。C盤,Documents and Settings\用戶名,一般是Administrator,\LocalSettings\Temp,系統(tǒng)默認(rèn)為隱藏目錄

3.IE瀏覽器臨時文件夾。C盤,Documents and Settings\用戶名\Local Settings\Temporary internet File系統(tǒng)默認(rèn)隱藏文件


一、簡介

這里介紹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/china/tmp/py_rs_file')

print(tmpdir) # /home/china/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('study hard and make progress')

# 方式二:生成自定義臨時文件(指定前綴、后綴、目錄、文件類型參數(shù),可指定其中一部分),suffix:后綴, prefix:前綴, dir:目錄, text:文件類型,True為文本,false為二進(jìn)制

tmpfile = tempfile.mkstemp(suffix='.txt', prefix='tp_', dir='/home/china/tmp/py_rs_file', text=True)[1]

print(tempfile) # /home/china/tmp/py_rs_file/tp_pn2973g0.txt

# 數(shù)據(jù)寫入

with open(tmpfile, 'w+') as t_f:

t_f.writelines('study hard and make progress')

3.2 生成自動刪除的臨時文件

# 方式一:創(chuàng)建臨時文件,文件關(guān)閉時自動刪除

tmpfile = tempfile.TemporaryFile(mode='w+t')

tmpfile.write('study hard and make progress everyday') #數(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('study hard and make progress everyday'.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/china/tmp/py_rs_file',delete=False) as tmpfile:

file_name = tmpfile.name

print(file_name) #/home/china/tmp/py_rs_file/tp_fcwpmh3l.txt

tmpfile.write('study hard and make progress everyday')

tmpfile.seek(0)

tmpTxt = tmpfile.read()

print(tmpTxt)

拓展知識:

臨時文件夾

工具/原料:電腦

1、第一在電腦中使用“windows+R組合鍵”進(jìn)入運(yùn)行中,輸入“temp”指令。

2、確定后就可進(jìn)入C盤中的temp臨時文件夾中,這里存放了系統(tǒng)運(yùn)行過程中**作的臨時文件。

3、點(diǎn)擊“修改時間”,按時間降序排列文件。

4、找到最新修改的文件,并將其**的電腦的桌面上。

5、對這個“tmp”后綴的文件重命名為“txt”后綴。

6、燃打開就可以查看里面的內(nèi)容了。

原創(chuàng)文章,作者:九賢生活小編,如若轉(zhuǎn)載,請注明出處:http://www.cddhlm.com/40356.html