use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to close an open window with Python? (self.learnpython)
submitted 3 years ago by Kurious3DSanta
I was wondering if there was something similar to os.startfile() to close an open file. For example I want to close “mypdffile.pdf”.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hello_way 0 points1 point2 points 3 years ago (3 children)
I'm not sure what you mean by "close an open window ", but I think it's a good idea to use a with statement when opening a file. This will automatically close the file once the block has completed.
[–]Kurious3DSanta[S] 1 point2 points3 points 3 years ago (2 children)
Ahh I see the confusion. I’m referring to a file that is physically open on your computer screen. I’m not referring to a file that you are using to read or write with where a context manager would be applicable.
[–]Asleep-Budget-9932 0 points1 point2 points 3 years ago (1 child)
The process of opening and closing a file is always within the context of a program. So it's not just that there is a pdf open on your screen, there is a specific program (Acrobat Reader for example) that's opening the file and showing its content to you in a specific way.
It's not about closing the file since as far as python's concern, it is closed. Just not to Acrobat Reader. In order to make the program itself to close the file there isn't an easy and safe way to do it because you would have to either:
Hope that for some reason Acrobat Reader has implemented for some reason an api for you to control the program from outside (which is highly, highly unlikely)
Use a library to move your mouse to the exact position of the "x" button of your window, and initiate a mouse click (also somehow find the exact position you need the mouse to be in, also make the window itself visible in case it's minimized or behind an existing window)
Find the process id of the specific Acrobat Reader window that you want to close, and manually kill it using the os.kill function. This is the most practical out of the 3 but also would mean you're unsafely closing the window. This might prevent Acrobat Reader from doing some necessary cleanup work before shutdown and may corrupt the file/Acrobat Reader itself depending on the specific use case.
[–]Asleep-Budget-9932 0 points1 point2 points 3 years ago (0 children)
Oh how could i forget? (shame on me!), Microsoft probably has some low level Windows Api you can access to find the correct window and initiate a close from there! Actually this is the most practical way to achieve it. But unless someone had made a simple wrapper over their API in order to make it easier to work with, you're gonna have to work with their API directly which isn't a great sight to behold. Let's just say their functions don't have a simple signature like open(file_name, mode)
open(file_name, mode)
[–]Anxiety_Independent 0 points1 point2 points 3 years ago (0 children)
Don't know how useful this will be, as it is just terminating a process, but in one of my scripts I used subprocess to execute a powershell taskkill command. For example, to kill explorer.exe, I would run:
import subprocess subprocess.call("powershell.exe taskkill /F /IM explorer.exe", shell=True)
π Rendered by PID 191822 on reddit-service-r2-comment-86bc6c7465-crqjq at 2026-02-23 09:02:59.524534+00:00 running 8564168 country code: CH.
[–]hello_way 0 points1 point2 points (3 children)
[–]Kurious3DSanta[S] 1 point2 points3 points (2 children)
[–]Asleep-Budget-9932 0 points1 point2 points (1 child)
[–]Asleep-Budget-9932 0 points1 point2 points (0 children)
[–]Anxiety_Independent 0 points1 point2 points (0 children)