1

I just want to get a way to open and close any particular folder through python code in windows 7 and above, Any Suggestion will be a great help. Thank you.

this is to open the directory

task = subprocess.Popen('explorer "C:\\', shell=True)
p = task.pid

this is to close through the PID

os.popen('TASKKILL /PID ' + str(p) + ' /F')

The problem is with the closing code it gives me and error:

ERROR:The proces "i.e 12086" not found.
CristiFati
  • 32,724
  • 9
  • 46
  • 70

1 Answers1

0

My go-to pattern for accessing a file is as follows:

with open("file.txt", "r") as file:
    # do stuff with file

This uses python context management to auto-close the file.

From here see how to open a file explorer from the windows command line:

import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')

http://www.geoffchappell.com/studies/windows/shell/explorer/cmdline.htm

To kill the process you do:

import os
os.system("taskkill /pid <ProcessID>")
Charles Landau
  • 3,971
  • 1
  • 6
  • 20