How do I close a file opened using os.startfile(), Python 3.6

前端 未结 3 502
青春惊慌失措
青春惊慌失措 2021-01-07 13:35

I want to close some files like .txt, .csv, .xlsx that I have opened using os.startfile().

I know this question asked earlier but I did not find any useful script fo

相关标签:
3条回答
  • 2021-01-07 14:07

    I believe the question wording is a bit misleading - in reality you want to close the app you opend with the os.startfile(file_name)

    Unfortunately, os.startfile does not give you any handle to the returned process. help(os.startfile)

    startfile returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application's exit status.

    Luckily, you have an alternative way of opening a file via a shell:

    shell_process = subprocess.Popen([file_name],shell=True) 
    print(shell_process.pid)
    

    Returned pid is the pid of the parent shell, not of your process itself. Killing it won't be sufficient - it will only kill a shell, not the child process. We need to get to the child:

    parent = psutil.Process(shell_process.pid)
    children = parent.children(recursive=True)
    print(children)
    child_pid = children[0].pid
    print(child_pid)
    

    This is the pid you want to close. Now we can terminate the process:

    os.kill(child_pid, signal.SIGTERM)
    # or
    subprocess.check_output("Taskkill /PID %d /F" % child_pid)
    

    Note that this is a bit more convoluted on windows - there is no os.killpg More info on that: How to terminate a python subprocess launched with shell=True

    Also, I received PermissionError: [WinError 5] Access is denied when trying to kill the shell process itself with os.kill

    os.kill(shell_process.pid, signal.SIGTERM)
    

    subprocess.check_output("Taskkill /PID %d /F" % child_pid) worked for any process for me without permision error See WindowsError: [Error 5] Access is denied

    0 讨论(0)
  • 2021-01-07 14:15

    In order to properly get the pid of children,you may add a while cycle

    
    import subprocess
    import psutil
    import os
    import time
    import signal
    shell_process = subprocess.Popen([r'C:\Pt_Python\data\1.mp4'],shell=True)
    parent = psutil.Process(shell_process.pid)
    while(parent.children() == []):
        continue
    children = parent.children()
    print(children)
    
    0 讨论(0)
  • 2021-01-07 14:22

    Based on this SO post, there's no way to close the file being opened with os.startfile(). Similar things are discussed in this Quora post.

    However, as is suggested in the Quora post, using a different tool to open your file, such as subprocess or open(), would grant you greater control to handle your file.

    I assume you're trying to read in data, so in regards to your comment about not wanting to close the file manually, you could always use a with statement, e.g.

    with open('foo') as f:
        foo = f.read()
    

    Slightly cumbersome, as you would have to also do a read(), but it may suit your needs better.

    0 讨论(0)
提交回复
热议问题