python终端模拟工具pexpect
pillow模块生成缩略图

mkstemp、mktemp、TemporaryFile区别

妙音 posted @ 2014年7月30日 13:07 in python , 5371 阅读

mkstemp

fd, name = tempfile.mkstemp 创建临时文件,并且将文件打开
>>> import tempfile
>>> tempfile.mkstemp()
(3, '/tmp/tmpkgWSR1')
查看/tmp目录,看到已经生成真实的文件
 
lsof查询打开的临时文件
[wyq@localhost tmp]$ lsof|grep tmp|grep python
python    8095           wyq    3u      REG               0,33         0     254593 /tmp/tmpkgWSR1
 
发现mkstemp不仅创建文件,而且将文件打开. 使用mkstemp很容易忘了这点,最终造成OSError: [Errno 24] Too many open files错误.
mkstemp返回的是文件描述和文件路径,并不常用,常用的是下面两个.

mktemp

name = tempfile.mktemp 返回一个临时文件的路径,但不创建该临时文件
>>> import tempfile
>>> tempfile.mktemp()
'/tmp/tmpPVidBM'
仅仅生成临时文件名

TemporaryFile

tempfile.TemporaryFile 返回文件对象(file-like)用于临时数据保存。当文件对象被close或者被del的时候,临时文件将从磁盘上删除
import time
with tempfile.TemporaryFile(mode='w+r') as f:
    f.write("=============")
    f.seek(0)
    print f.read()
    time.sleep(10)
TemporaryFile并未在/tmp目录中创建临时文件,应该只存在与内存中.
 
Avatar_small
依云 说:
2014年9月02日 14:16

TemporaryFile 在打开文件之后立即删除了文件,所以在文件系统中看不到。lsof 还是可以看到的:

4 REG 0x1f 0 17699285 /tmp/tmpvcndbo4q (deleted)

Avatar_small
Marijuana dispensary 说:
2020年11月05日 19:40

Organizing your immediate environment will make it easier for you to remember things! Keeping your keys, wallet, cell phone and other frequently used articles all together in the same place will prevent you from having to remember where they are. Since scent can improve memory, keep your favorite scented candle in this same central location! All of this will improve your memory and save you much stress and hassle.

Avatar_small
no excuses runner 说:
2020年12月15日 16:25

I needed to thank you for this incredible read!! I unquestionably adored each and every piece of it. I have you bookmarked your site to look at the new stuff you post. pretty gaming

Avatar_small
Situs Judi Online Pk 说:
2020年12月19日 01:35

This is a great high resolution screen which you have shared for the users. Making a website is not an easy task but managing a good website is really a hard work. As far as this website is concerned, I am very happy. jovell price

Avatar_small
I definitely enjoyin 说:
2020年12月19日 20:24

When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. download 918Kiss APK

Avatar_small
UBSE Question Paper 说:
2020年12月20日 15:05

Students going to appear for intermediate exams must download the UK board question paper of class 12 to prepare better. Students can also download question papers for all subjects in this article along with previous year papers. Solving UK board 12th question papers will aid students to ace the exam. The Uttarakhand 12th question papers will introduce students with exam pattern, marking scheme, types and number of questions asked and the difficulty level of the UK Board 12th exam. UK Board 12th question papers 2020-21 will be published on the official website after the exams. But students can solve previous years’ question papers along with UK 12th model papers 2021 to analyze their preparation level. Read the complete article to know more about UBSE Question Paper 2021 for 12th.

Avatar_small
먹튀검증업체 说:
2020年12月20日 17:43

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! 먹튀검증업체

Avatar_small
Linus Pauling 说:
2020年12月21日 15:59

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. Linus Pauling

Avatar_small
Situs Judi Online Pk 说:
2020年12月22日 00:49

Hi - Visit this Informative Article for Wix vs squarespace 2020 | 10 Crucial Differences You Should Know

Avatar_small
penrose 说:
2020年12月22日 21:09

Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! <a href="https://singaporepropertylaunch.com.sg/properties/penrose-condo/">penrose</a>

Avatar_small
penrose 说:
2020年12月22日 21:10

Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! penrose

Avatar_small
Situs Judi Online Pk 说:
2020年12月24日 02:35

High Quality Toupees For Men human hair toupee sales - Honourhair

Avatar_small
Situs Judi Online Pk 说:
2020年12月25日 14:25

I know your aptitude on this. I should say we ought to have an online discourse on this. Composing just remarks will close the talk straight away! What's more, will confine the advantages from this data. بازی انفجار چگونه است

Avatar_small
visit Pussy888 offic 说:
2020年12月26日 16:02

I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer.Thanks. visit Pussy888 official website

Avatar_small
ryse residences cond 说:
2020年12月26日 18:47

Our credit repair services work to fix past credit mistakes and verify credit report accuracy. Talk to a credit repair expert today!  One Bernam Condo

Avatar_small
agen idn poker 说:
2020年12月29日 16:02

Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback. agen idn poker

Avatar_small
no excuses runner 说:
2020年12月31日 16:59

Your post has those facts which are not accessible from anywhere else. It’s my humble request to u please keep writing such remarkable articles kashan rugs for sale

Avatar_small
no excuses runner 说:
2020年12月31日 19:10

Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. ดูอนิเมะ

Avatar_small
no excuses runner 说:
2020年12月31日 19:57

This article is an engaging abundance of enlightening information that is intriguing and elegantly composed. I praise your diligent work on this and thank you for this data. You have what it takes to get consideration. продать скины кс го

Avatar_small
no excuses runner 说:
2020年12月31日 20:51

One present why galore businesses opt for postcards is because they are overmuch cheaper to be prefab and this can forbear a lot of expenses on the lengthened run. 918 kiss apk

Avatar_small
no excuses runner 说:
2020年12月31日 21:25

One present why galore businesses opt for postcards is because they are overmuch cheaper to be prefab and this can forbear a lot of expenses on the lengthened run. forex signals

Avatar_small
Betfair Bonus 说:
2021年1月04日 16:16

Yacht Rental Cyprus|Limos Cyprus|Yachts for hire Cyprus|Limousines Cyprus|Yacht excursions Cyprus|Boat Rental Cyprus | https://www.cyprusvipservice.com/ pays personal and professional attention to our respected clientele. We treat you as a VIP, just like you deserve. We ensure that you will enjoy your yacht or limo trip, to the fullest. Yachts for hire Cyprus

Avatar_small
free movies streamin 说:
2021年1月06日 21:06

Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. free movies streaming sites

Avatar_small
토토 说:
2021年1月07日 21:26

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. 토토

Avatar_small
Kişiye Özel 说:
2021年1月09日 18:59

This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! Kişiye Özel

Avatar_small
Imamia Quran Academy 说:
2023年7月23日 06:05

 

Your blog post deserves all of the positive feedback it’s been getting. I am from Imamia Quran Academy. The Shia Quran Madrassa online is a secure place for kids and adults to learn and memorize the Quran. They teach the real translation of the Holy Quran and the interpretation of the Holy Quran under the guidance of expert Quranic teachers.

Avatar_small
ahmed 说:
2024年3月11日 14:34

In conclusion, SEO is not just a buzzword – it's a fundamental component of your digital marketing strategy. By partnering with VantFanzine, you gain access to expert SEO services designed to enhance your online visibility, drive organic traffic, and maximize your ROI. Let us help you unlock the full potential of SEO and propel your brand to new heights in the digital realm. <a href="https://ventsfanzine.com/2024/02/15/decoding-02045996870-why-its-popular-inside-the-uk/">02045996870</a>

Avatar_small
ahmed 说:
2024年3月11日 14:36

Continuous monitoring and analysis are essential for refining and optimizing your SEO strategy. We leverage advanced analytics tools to track key metrics such as traffic, rankings, and conversions, allowing us to identify areas for improvement and capitalize on emerging opportunities. 02045996870

Avatar_small
https://ventsfanzine 说:
2024年3月13日 00:10

In an era dominated by digital connectivity, VentsFanzine has successfully navigated the challenges of the online world. Social media integration, interactive forums, and a user-friendly website have transformed VentsFanzine into a virtual haven for fans

Avatar_small
Chrisley Knows Best 说:
2024年3月19日 03:43

Delve into the captivating world of 'Chrisley Knows Best' and discover the vibrant essence of its beloved daughter. https://busniesstomark.co.uk/chrisley-knows-best-daughter-dies/


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter