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 6567355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:19:36+00:00 2026-05-25T14:19:36+00:00

I need to create a file on the server, serve it to the client,

  • 0

I need to create a file on the server, serve it to the client, and I would prefer to delete the file from the server afterwards.

Here’s what I’m currently trying:

def myaction():
    fs = facts.generatefacts(int(low),int(high),int(amount),op)
    filename = 'test.txt'
    FILE = open(filename,'w')
    FILE.writelines('my\nstuff\nhere')
    FILE.close()
    RETURN_FILE = open(filename,'r')
    return serve_fileobj(RETURN_FILE,disposition='attachment',
                         content_type='.txt',name=filename)

myaction.exposed = True

There are several things about this that I don’t like. I wouldn’t think that I would have to open the file twice, for example. I would expect that there is a way to write content directly to the response object, without ever creating a file object, but that isn’t my question today.

The above code accomplishes what I want, but leaves a file behind. If I delete the file before returning the response, then (of course) the file isn’t found.

Is there a way to remove this file as soon as it has been served?

I’m coming from the Java world, so I’m a bit disoriented, and any other suggestions to improve the above are appreciated.

  • 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-25T14:19:37+00:00Added an answer on May 25, 2026 at 2:19 pm

    1) You can move file to temporary folder and remove all files older 0.5 hours

    2) You can try

    result = serve_fileobj(RETURN_FILE,disposition='attachment',
                             content_type='.txt',name=filename)
    os.unlink(filename)
    return result
    

    3) Try to use StringIO file object that can wrap string to look like a file.

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

Sidebar

Related Questions

I need to create a batch file which will copy web log files from
I need to create file sharing and chatting system like Microsoft Groove under .NET
We need to create a file management interface that integrates with the clients current
I understand that we need to create MXML file to define a view. Suppose
I need to create a batch file which starts multiple console applications in a
I need to create a BAT file to run an application through telnet, but
We need to create an .xls file in ASP .NET which will be imported
I need to create an excel file via C#. I have read a few
I need to create a batch file that reads a file with one line
I need to create a batch file that changes path to a particular folder

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.