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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:40:20+00:00 2026-06-05T19:40:20+00:00

I have written a Python script that will generate a series of graphs and

  • 0

I have written a Python script that will generate a series of graphs and then generate a PDF report that contains the graphs. The intention is that this will be generated on a server. I am using ReportLab to generate the PDF. The script is being run on a machine that is running Windows 7.

At the beginning of the script a list to store the ReportLab flowables and a list to store path of each figure is set.

Story = []
FigList = []

Each graph is generated using Matplotlib and saved as a PNG. The image is appended to Story and the path of the file is added to FigList.

fig = plt.figure(1)
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y)
fname = "figure.png"
fig.savefig(fname)
FigList.append(fname)
Story.append(Image(fname))

This is repeated for a number of images (approximately 10 – 15 in total). At the end of the script the document is built

doc = SimpleDocTemplate("report.pdf",
                        pagesize=A4,
                        rightMargin=cm,
                        leftMargin=cm,
                        topMargin=cm,
                        bottomMargin=cm)

doc.build(Story)

After this I wish to remove all the PNG files that have been created using

for f in FigList:
    if os.path.exists(f): os.remove(f)

However, I am presented with the following error

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: ‘image.png’

I am assuming here that the document creation process is being performed in a separate thread and so when the script attempts to remove the figure files they are still marked as open by the file manager.

It is important that these image files are deleted before the process finishes as this process may be executed many times, therefore the temporary image files will soon take up too much space.

Is there a way I can get the script to wait for all the images to be closed before proceeding to delete the files?

  • 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-06-05T19:40:21+00:00Added an answer on June 5, 2026 at 7:40 pm

    It would seem that the problem can be solved by assigning Image(...) to a variable before appending it to Story and then deleting it afterwards. If we consider the example in the question, this modification becomes

    fig = plt.figure(1)
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(x, y)
    fname = "figure.png"
    fig.savefig(fname)
    FigList.append(fname)
    img = Image(fname)
    Story.append(img)
    del img
    

    Note: This is not related to the file object created by savefig().

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

Sidebar

Related Questions

I have a mechanize script written in python that fills out a web form
I have written up a python script that allows a user to input a
I have a python script that parses an XML file that contains part information
I have written a Python script that currently is designed to handle traditional CGI
I have an installation script written in Python (in Linux) that runs as root
I have written a short python script that opens Google music in web view
I am new to python and i have written a script that converts a
Greetings. I have written a little python script that calls MySQL in a subprocess.
Does anyone know of an already-written Python script, tool, or editor that will check
Is there a module written purely in Python that will allow a script to

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.