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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:51:00+00:00 2026-05-28T19:51:00+00:00

This is part of a python program under windows environment. I am trying to

  • 0

This is part of a python program under windows environment. I am trying to do the following:
from an html, I want to create a pdf and then open it:

    #create the pdf
    os.system("start wkhtmltopdf.exe result.html %s%s" %(output_directory, pdf_document_name))
    #open the current pdf
    os.system("start %s%s" %(output_directory, pdf_document_name))

The issue is that sometimes the script of creating the pdf is slower, so I get an error from the terminal saying that there is no file with such name.
I want to ask whether it’s possible to call the pdf open only when the creation is successful and finished. I know how to do it by invoking a time.sleep() , but I think that’s not too professional.
Many thanks,
D

  • 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-28T19:51:01+00:00Added an answer on May 28, 2026 at 7:51 pm

    Regardless of the method you use to execute shell commands (subprocess, os.system, etc.), you should verify that your file exists before trying to open it. You can then instrument the delay into the File-Exists-Else-Wait-Repeat loop before trying to open it.
    You can do this using os.path.exists():

    #create the pdf
    os.system("start wkhtmltopdf.exe result.html %s%s" %(output_directory, pdf_document_name))
    
    #loop until pdf exists, but add some timer to avoid endless repetition
    maxiterations = 60
    exists = False
    for i in range(maxiterations):
        if os.path.exists(os.path.join(output_directory, pdf_document_name)):
            exists = True
            break;
        time.sleep(1)
    #open the current pdf
    if exists:
        os.system("start %s%s" %(output_directory, pdf_document_name))
    else:
        print 'Could not find file %s to open' % os.path.join(output_directory, pdf_document_name)
    

    The other thing to look at is that this introduces a security vulnerability, as the file could change in the time that passed between your verifying it and opening it (e.g. it could have been replaced with malicious code).
    Another way to handle this is to try to open it within a try… except… block, but this doesn’t really resolve the security issue (the file could have been replaced between creation and your attempt at opening it anyway).

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

Sidebar

Related Questions

I have a python program that does something like this: Read a row from
In some part of my python program , I need to do this -
As part of a large python program I have the following code: for arg
This module is part of a simple todo app I made with Python... def
I am trying to learn and writting this part of code. while testing few
I'd like to know what you think about this part of our program is
I'm using this line in a SPARQL query in my python program: FILTER regex(?name,
I want to do exactly what this guy did: Python - count sign changes
I'm trying to convert a simple C program into Python but as I don't
I've a part of my python program which is generated, the generated codes contains

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.