Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:55:55+00:00 2026-05-26T03:55:55+00:00

For a small python script I would like to use a temporary file with

  • 0

For a small python script I would like to use a temporary file with the tempfile module. Somehow it does not give the expected behavior and I don’t know what I am doing wrong or if this is a bug:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tmp = tempfile.TemporaryFile()
>>> tmp.read()
''
>>> tmp.write('test')
>>> tmp.read()
'P\xf6D\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ [ommitted]'

alternatively I tried with text mode only but the behavior is still strange:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tmp = tempfile.TemporaryFile('w+t')
>>> tmp.read()
''
>>> tmp.write('test')
>>> tmp.read()
'\x00\xa5\x8b\x02int or long, hash(a) is used instead.\n    i\x10 [ommitted]'
>>> tmp.seek(0)
>>> tmp.readline()
'test\x00\xa5\x8b\x02int or long, hash(a) is used instead.\n'

Any help is appreciated!


Additional information: Python 2.7.2 (32 bit) from the current Python XY distribution running on a Windows 7 Enterprise x64 machine. In a test run python created the temporary file name “tmpvyocxj” in my temporary directory under D:\temp\myusername with several other python processes running. The commands where typed in, I have not tried to reproduce it in a script. The behavior is unchanged with no other python processes running.


Update:
This behavior is not limited to the tempfile module but also for the normal file.read() and file.write() operations. According to the CPython guys both functions only call the underlying libc fread() routines. In the C-standard the exact behavior of read after write without a seek or flush in between is undefined, i.e. every implementation can lead to different results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:55:55+00:00Added an answer on May 26, 2026 at 3:55 am

    I just reproduced this behavior in Python 2.7.1 on Windows XP.

    This appears to be a bug that only occurs when you attempt to read without seeking first.

    That is:

    >>> tmp.write('test')
    >>> tmp.seek(0)
    >>> tmp.read()
    'test'
    

    vs.

    >>> tmp.write('test')
    >>> tmp.read()
    'x\x01\x98\x00pfile.pyR\x05\x00\x00\x00G\x01\x00\x00s\x12 [blah blah blah]'
    >>> tmp.seek(0)
    >>> tmp.read()
    'testx\x01\x98\x00pfile.pyR\x05\x00\x00\x00G\x01\x00\x00s\x12 [blah blah blah]'
    

    EDIT:

    For kicks, I also checked:

    1. Windows 7, again 2.7.1 (same build as my XP install), and the same behavior was seen as on XP.
    2. Cygwin version 2.6.5, and this bug was not present.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to build a small python script that basicaly does the reverse
I have a small python script which i use everyday......it basically reads a file
For some small programs in Python, I would like to set, store and retrieve
I'd like to create a small script that basically does this: run program1.exe -->
I have some small python 2.6 scripts built.... Now, I would like run them
I have a small awk script that does some in-place file modifications (to a
I've created a small python script to toggle between two files I'm using for
Im making a small python script to upload files on the net. The script
I am looking to include a reference to a non-elisp file (a small Python
I've wrote a small cryptographic module in python whose task is to cipher a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.