I am given a SpooledTemporaryFile through FastAPI's UploadFile. My goal is to write it to disk so I can extract it a prosses it's contents later. This seems simple, but I am having problems saving the file. I have made a test script below: file_path = "new.tar.gz" old_file = open("old.tar.gz", "wb") with open(file_path, mode="xb") as new_file: new_file.write(old_file.read()) It works great as long as mode="wb". But the moment I change it to w+b like a TemporaryFile, the resulting file becomes unreadable by tar -xvf. I don't have the ability to change the mode, as it happens inside FastAPI. Why are the files mangled, and how do I stop it? I believe this is a generic problem, and is not necessary FastAPI specific. If I'm wrong, just comment and I'll change it. Continue reading...