-1

This seems like such a simple thing, but I've searched for half an hour and can't find an answer anywhere.

Let's say I have a PDF document already open in Windows 10. Let's say that the PDF is called 'test.pdf' and it is stored in the working directory of a python script I'm to create. I want the python script to close this PDF document that is already open prior to the script being run.

Everything I've researched regarding closing files with python involves having opened the file in python, but that is not my situation. The file is not being opened with python. It was already opened prior to any python code being run and I want the python script to close the PDF file.

LetEpsilonBeLessThanZero
  • 2,395
  • 2
  • 12
  • 22
  • 4
    You want to break someone else's file handle? That's generally ill-advised. – g.d.d.c Oct 25 '18 at 17:35
  • Check out this https://stackoverflow.com/questions/11114492/check-if-a-file-is-not-open-not-used-by-other-process-in-python/11115521 – misterflister Oct 25 '18 at 17:41
  • FWIW, this problem seems to be unique to Windows NT and filesystems with VMS heritage. In most Unix-y filesystems, file handles are not exclusive, and another process can read, write, or delete a file while your process is using it. This is why Unix systems can update (most) parts of the system without a reboot, but Windows machines need a reboot for practically any software update (since rebooting closes everything, programs get a chance to modify/replace files on the next boot cycle before they get opened again). – Daniel Pryden Oct 25 '18 at 18:27
  • Well, I feel like it's appropriate in my case. I am using the 'reportlab' package to generate a PDF. In order to test my code, I need to open and close the PDF file that I'm generating every time I run the script. I want the PDF to be open after the script is done producing the PDF, which means it'll already be open the next time I go to run script unless I manually close the PDF before running the code again. – LetEpsilonBeLessThanZero Oct 25 '18 at 18:33
  • 1
    It isn't *your program* that has the PDF file open; it is therefore not even meaningful to talk about your program closing it. What you need to do is tell *the PDF viewer* to close the file; there isn't any general way to do that, short of forcibly killing the viewer program. – jasonharper Oct 25 '18 at 19:10

1 Answers1

1

This sound like an X/Y problem: what you really want according to your comments to the question, is to modify a PDF file without closing the PDF viewer that has that file open.

What should fit your needs is to use a PDF viewer for Windows that can handle live updates. You can google that and look for possible applications.

See also this question on superuser with a similar topic. I personally used my browser (chrome and later firefox) to view PDFs that I was modifying programatically without closing the file in the browser.

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • Thank you. I'm going to accept this as the answer, because I was able to find a PDF viewer that accomplishes what I want. I never would have thought that the real problem was Adobe Acrobat just being shit software. You're right, this is a X/Y problem, but in my defense I've had "work flow" questions closed on me in the past for being too subjective. That's why I chose a question that would have some sort of objective answer. – LetEpsilonBeLessThanZero Oct 25 '18 at 20:01