lanoxx
lanoxx

Reputation: 13051

Windows Explorer does not show file size when writing file with Python

When I write to a file with Python 3 on Windows, the Windows Explorer keeps showing that the file size is 0 KB until the script has finished and then it suddenly jumps to 2.4GB. When I use other programs that write to a file then I can view how the files size increases in Windows Explorer. Is there something my script needs to be doing so that the files size gets updated in windows explorer?

My script has roughtly the following structure:

resultStream = open(target, "w", newline="\r\n", 
                    encoding="ISO-8859-1")
for lineno, line in enumerate(reader):
    resultStream.write(data)
    # flush every 1000 lines
    if lineno % 1000 == 0:
        resultStream.flush()

Then I close the result stream when I am finished.

Upvotes: 1

Views: 126

Answers (1)

lanoxx
lanoxx

Reputation: 13051

I just found this blog post which gives some explanation to why this is happening. Looks like it has nothing to do with Python or with the way I build my script. The reason simply seems to be that the file system (e.g. NTFS) does not update the meta data of the file until the file handle is closed.

http://blogs.msdn.com/b/oldnewthing/archive/2011/12/26/10251026.aspx

Upvotes: 2

Related Questions