title: Python Selenium --user-data-dir option ERROR: could not remove old devtools port file
body: <h2>--user-data-dir option</h2>
  
  <p>I tried to open Chromedriver with --user-data-dir option but following error comes out. I've tried this in many ways for 1 month and still don't have clue. Please help me!</p>
  
  <p>Error message is:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):
      File "C:\Users\owner\Desktop\MouseWithoutBorders\AutoCheckin.py", line 15, in 
        driver = webdriver.Chrome(executable_path=r"C:\Users\owner\Desktop\MouseWithoutBorders\chromedriver.exe", chrome_options=opts)
      File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in <strong>init</strong>
        desired_capabilities=desired_capabilities)
      File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in <strong>init</strong>
        self.start_session(capabilities, browser_profile)
      File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 251, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
        self.error_handler.check_response(response)
      File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
    <strong>selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at  C:\Users\owner\AppData\Local\Google\Chrome\ is still attached to a running Chrome or Chromium process.</strong>
      (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17738 >x86_64)</p>
  </blockquote>
  
  <p>and My python test code is:</p>
  
  <pre><code>import time
  from selenium import webdriver
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.common.action_chains import ActionChains
  from selenium.webdriver.chrome.options import Options
  from myidez import ID, PW
  
  opts = webdriver.ChromeOptions()
  opts.add_argument('--start-maximized')
  opts.add_argument('--headless')
  opts.add_argument('--no-sandbox')
  opts.add_argument('--disable-dev-shm-usage')
  opts.add_argument('--disable-gpu')
  opts.add_argument("--user-data-dir= C:\\Users\\owner\\AppData\\Local\\Google\\Chrome\\User Data")
  driver = webdriver.Chrome(executable_path=r"./chromedriver.exe", chrome_options=opts)
  </code></pre>
  
  <p>I have found this Error comes from chromium code
  references:
  <a href="https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/chrome_launcher.cc" rel="noreferrer">https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/chrome_launcher.cc</a>
  <a href="https://peter.sh/experiments/chromium-command-line-switches/" rel="noreferrer">https://peter.sh/experiments/chromium-command-line-switches/</a></p>
  
  <pre><code>Status RemoveOldDevToolsActivePortFile(const base::FilePath&amp; user_data_dir) {
    base::FilePath port_filepath = user_data_dir.Append(kDevToolsActivePort);
    // Note that calling DeleteFile on a path that doesn't exist returns True.
    if (base::DeleteFile(port_filepath, false)) {
      return Status(kOk);
    }
    return Status(
        kUnknownError,
        std::string("Could not remove old devtools port file. Perhaps "
                    "the given user-data-dir at ") +
            user_data_dir.AsUTF8Unsafe() +
            std::string(" is still attached to a running Chrome or Chromium "
                        "process."));
  </code></pre>