avatarLiu Zuo Lin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1605

Abstract

lder <span class="hljs-keyword">in</span> folders: file.stream.seek(<span class="hljs-number">0</span>) file.save(folder)</pre></div><p id="80eb">After I added this line <code>file.stream.seek(0)</code>, my code worked as intended. The file would still save in all relevant folders, but this time, the contents of each file would be correct.</p><h1 id="b7c7">Why the original code doesn’t work</h1><div id="e35b"><pre><span class="hljs-keyword">for</span> folder <span class="hljs-keyword">in</span> folders: file.save(folder)</pre></div><p id="4323"><code>file</code> can only <code>.save()</code> once. If we <code>.save()</code> it a second time (without the above workaround), it will simply save an empty file.</p><p id="4af5">For some reason, there’s some sort of cursor inside <code>file</code>. And if we <code>.save()</code> it, the cursor goes to the end of <code>file</code>. And if we try to <code>.save()</code> it again, the cursor doesn’t move as it is already at the end! Leading to the empty file being saved.</p><h1 id="173b">What the solution does</h1><div id="e607"><pre><span class="hljs-keyword">for</span> folder <span class="hljs-keyword">in</span> folders: file.stream.seek(<span class="hljs-number">0</span>) file.save(folder)</pre></div><p id="9fbf">The line <code>file.stream.seek(0)</code> resets the cursor back to the start of the file. So that whenever <code>.save()</code> happens, the cursor is at the start of the file, and all the content in the file gets written.</p><h1 id="e96d">Moral of the story</h1><p id="538b">I had lots of other stuff to handle, so I

Options

didn’t dig <i>deep </i>into why it was designed like this.</p><p id="572a">But if you’re working with Python Flask, and need to save a FileStorage object to multiple directories, remember to add the <code>file.stream.seek(0)</code> line.</p><h1 id="fedf">Some Final words</h1><p id="c58a"><i>If this story provided value and you wish to show a little support, you could:</i></p><ol><li><i>Clap 50 times for this story (this really, really helps me out)</i></li><li><i>Sign up for a Medium membership using <a href="https://zlliu.medium.com/membership">my link</a> ($5/month to read unlimited Medium stories)</i></li></ol><p id="94b2"><b>My Ebooks: <a href="https://zlliu.co/books">https://zlliu.co/ebooks</a></b></p><p id="07e5"><b>My LinkedIn: <a href="https://www.linkedin.com/in/zlliu/">https://www.linkedin.com/in/zlliu/</a></b></p><p id="960e"><b>My Workspace Setup: <a href="https://zlliu.co/workspace">https://zlliu.co/workspace</a></b></p><div id="f45f" class="link-block"> <a href="https://zlliu.medium.com/subscribe"> <div> <div> <h2>Get an email whenever Liu Zuo Lin publishes.</h2> <div><h3>Get an email whenever Liu Zuo Lin publishes. By signing up, you will create a Medium account if you don't already have…</h3></div> <div><p>zlliu.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*RbXfMaJ98Rjko_RA)"></div> </div> </div> </a> </div></article></body>

I Met A Funny Issue With Flask’s FileStorage At Work Today

# Take Note if you’re working with Python Flask

So I was fixing a bug today at work.

Desired outcome — user uploads some file, and we save it in multiple directories without problem

Actual outcome — user uploads some file, and we save it in multiple directories. Said file appears in all directories. All but one file is empty.

How My Initial Code Was Like

Not gonna post my exact code cus Company policy and stuff. But this was what it kinda looked like:

for folder in folders:
  file.save(folder)

# folder is a string value representing an actual folder path
# file is a FileStorage object 

Assuming I had 3 folders folder1 folder2 and folder3, the file did end up appearing in the folders.

folder1/file.txt
folder2/file.txt
folder3/file.txt

But only folder1/file.txt had stuff inside. folder2/file.txt and folder3/file.txt were just empty text files.

The Solution That Worked

for folder in folders:
  file.stream.seek(0)
  file.save(folder)

After I added this line file.stream.seek(0), my code worked as intended. The file would still save in all relevant folders, but this time, the contents of each file would be correct.

Why the original code doesn’t work

for folder in folders:
  file.save(folder)

file can only .save() once. If we .save() it a second time (without the above workaround), it will simply save an empty file.

For some reason, there’s some sort of cursor inside file. And if we .save() it, the cursor goes to the end of file. And if we try to .save() it again, the cursor doesn’t move as it is already at the end! Leading to the empty file being saved.

What the solution does

for folder in folders:
  file.stream.seek(0)
  file.save(folder)

The line file.stream.seek(0) resets the cursor back to the start of the file. So that whenever .save() happens, the cursor is at the start of the file, and all the content in the file gets written.

Moral of the story

I had lots of other stuff to handle, so I didn’t dig deep into why it was designed like this.

But if you’re working with Python Flask, and need to save a FileStorage object to multiple directories, remember to add the file.stream.seek(0) line.

Some Final words

If this story provided value and you wish to show a little support, you could:

  1. Clap 50 times for this story (this really, really helps me out)
  2. Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)

My Ebooks: https://zlliu.co/ebooks

My LinkedIn: https://www.linkedin.com/in/zlliu/

My Workspace Setup: https://zlliu.co/workspace

Python
Programming
Flask
Python Flask
Recommended from ReadMedium