Python script to check dir not working
<p>I am stuck with my script at a point. The script is this</p>

<pre><code>import subprocess
import os
def Windows():
    SW_MINIMIZE = 6
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_MINIMIZE
    print(os.path.isdir("C:\Program Files (x86)"))
    while True:
        try:
            subprocess.Popen(r'C:\Program Files (x86)\Mozilla   Firefox\firefox.exe', startupinfo=info)
        except WindowsError:
            subprocess.Popen(r'C:\Program Files   (x86)\Google\Chrome\Application\chrome.exe', startupinfo=info)
    else:
        try:
            subprocess.Popen(r'C:\Program Files\Mozilla Firefox\firefox.exe', startupinfo=info)
        except WindowsError:
            subprocess.Popen(r'C:\Program Files\Google\Chrome\Application\chrome.exe', startupinfo=info)  
</code></pre>

<p>What I want to do is check if the computer is 64 bit or 32 bit (as I want to open the browser without a window using <code>subprocess</code>.) to locate the browsers <code>chrome</code> or <code>firefox</code>, depending on which one the user has ( I am assuming that they have either one of them). Since the path for chrome and firefox varies in 64 vs 32 bit computers (Program Files and Program Files (x84)), I came up with this script which detects if x86 folder exists or not. If it does, it continues on the folder for searching for the browsers. However, if it doesn't, it assumes it is 32-bit and searches for <code>Program Files</code> folder and in that folder it searches for the browsers.
However, when I run the script I get this error</p>

<pre><code>Traceback (most recent call last):
  File "C:\Users\Charchit\Desktop\via.py", line 29, in &lt;module&gt;
    Windows()
  File "C:\Users\Charchit\Desktop\via.py", line 13, in Windows
    subprocess.Popen(r'C:\Program Files    (x86)\Google\Chrome\Application\chrome.exe', startupinfo=info)
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
</code></pre>

<p>However, in my script it should not even go to <code>while True</code> Section because I have a 32 bit system and <code>x86</code> folder doesn't exist!</p>

<p>You're not actually checking if <code>os.path.isdir("C:\Program Files (x86)")</code>. You're just printing it.</p>

<p>Instead of </p>

<pre><code>print(os.path.isdir("C:\Program Files (x86)"))
while True:
</code></pre>

<p>You need to do</p>

<pre><code>if os.path.isdir(r"C:\Program Files (x86)"):
</code></pre>

<p>Side note: </p>

<p>Both chrome and firefox traditionally place themselves on the path, so there's a good chance you can just do <code>subprocess.Popen('firefox.exe')</code> / <code>subprocess.Popen('chrome.exe')</code>.</p>

python python-2.7 subprocess