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
  • 38,250
  • 9
  • 50
  • 87

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
  • 4,187
  • 1
  • 8
  • 24