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

  • SEARCH
  • Home
  • 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 7078647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:34:25+00:00 2026-05-28T06:34:25+00:00

Assume there is a file test.txt containing a string ‘test’ . Now, consider the

  • 0

Assume there is a file test.txt containing a string 'test'.

Now, consider the following Python code:

f = open('test', 'r+')
f.read()
f.truncate(0)
f.write('passed')
f.flush();

Now I expect test.txt to contain 'passed' now, however there are additionally some strange symbols!

Update: flush after truncate does not help.

  • 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-28T06:34:26+00:00Added an answer on May 28, 2026 at 6:34 am

    This is because truncate doesn’t change the stream position.

    When you read() the file, you move the position to the end. So successive writes will write to file from that position. However, when you call flush(), it seems not only it tries to write the buffer to the file, but also does some error checking and fixes the current file position. When Flush() is called after the truncate(0), writes nothing (buffer is empty), then checks the file size and places the position at the first applicable place (which is 0).

    UPDATE

    Python’s file function are NOT just wrappers around the C standard library equivalents, but knowing the C functions helps knowing what is happening more precisely.

    From the ftruncate man page:

    The value of the seek pointer is not modified by a call to ftruncate().

    From the fflush man page:

    If stream points to an input stream or an update stream into which the most recent operation was input, that stream is flushed if it is seekable and is not already at end-of-file. Flushing an input stream discards any buffered input and adjusts the file pointer such that the next input operation accesses the byte after the last one read.

    This means if you put flush before truncate it has no effect. I checked and it was so.

    But for putting flush after truncate:

    If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() causes any unwritten data for that stream to be written to the file, and the st_ctime and st_mtime fields of the underlying file are marked for update.

    The man page doesn’t mention the seek pointer when explaining output streams with last operation not being input. (Here our last operation is truncate)

    UPDATE 2

    I found something in python source code: Python-3.2.2\Modules\_io\fileio.c:837

    #ifdef HAVE_FTRUNCATE
    static PyObject *
    fileio_truncate(fileio *self, PyObject *args)
    {
        PyObject *posobj = NULL; /* the new size wanted by the user */
    #ifndef MS_WINDOWS
        Py_off_t pos;
    #endif
    
    ...
    
    #ifdef MS_WINDOWS
        /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
           so don't even try using it. */
        {
            PyObject *oldposobj, *tempposobj;
            HANDLE hFile;
    
    ////// THIS LINE //////////////////////////////////////////////////////////////
            /* we save the file pointer position */
            oldposobj = portable_lseek(fd, NULL, 1);
            if (oldposobj == NULL) {
                Py_DECREF(posobj);
                return NULL;
            }
    
            /* we then move to the truncation position */
            ...
    
            /* Truncate.  Note that this may grow the file! */
            ...
    
    ////// AND THIS LINE //////////////////////////////////////////////////////////
            /* we restore the file pointer position in any case */
            tempposobj = portable_lseek(fd, oldposobj, 0);
            Py_DECREF(oldposobj);
            if (tempposobj == NULL) {
                Py_DECREF(posobj);
                return NULL;
            }
            Py_DECREF(tempposobj);
        }
    #else
    
    ...
    
    #endif /* HAVE_FTRUNCATE */
    

    Look at the two lines I indicated (///// This Line /////). If your platform is Windows, then it’s saving the position and returning it back after the truncate.

    To my surprise, most of the flush functions inside the Python 3.2.2 functions either did nothing or did not call fflush C function at all. The 3.2.2 truncate part was also very undocumented. However, I did find something interesting in Python 2.7.2 sources. First, I found this in Python-2.7.2\Objects\fileobject.c:812 in truncate implementation:

     /* Get current file position.  If the file happens to be open for
     * update and the last operation was an input operation, C doesn't
     * define what the later fflush() will do, but we promise truncate()
     * won't change the current position (and fflush() *does* change it
     * then at least on Windows).  The easiest thing is to capture
     * current pos now and seek back to it at the end.
     */
    

    So to summarize all, I think this is a fully platform dependent thing. I checked on default Python 3.2.2 for Windows x64 and got the same results as you. Don’t know what happens on *nixes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I wish to create (or overwrite) the following file :- C:\Temp\Bar\Foo\Test.txt Using the
Let's assume I have the following file - template.php : <?php $string = 'Hello
Is there an easier way to test if a file system item is a
Assume I have a sample source file, test.c, which I am compiling like so:
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
Please consider the following URLs: http://www.mydomain.com/a/test.php https://www.mydomain.org/a/b/test.php http://www.mydomain.co.nr/a/b/c/test.php https://www.mydomain.com/a/b/c/d/test.php http://www.mydomain.co.uk/a/b/c/d/e/test.php https://www.mydomain.co.au.nm/a/b/c/d/e/f/test.php?var1=test1&var2=test2 Now I want
I have the following section of code that acts on values in <Test></Test> nodes
Let's consider python (3.x) scripts: main.py: from test.team import team from test.user import user
I assume there must be a system and language independent way to just stick
How do I have a script run every, say 30 minutes? I assume there

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.