Repository: pyinstaller/pyinstaller

Issue:
Changing from -F to -D on consecutive runs causes crash
MacOS (have not tried Win, Linux; may well fail there too), PyInstaller 3.3.1. Demonstrate as follows.

~~~~
$  pyinstaller --version
3.3.1
$ cat > hello.py
import sys
print( 'hello',sys.argv[1] ) ^D
$ python hello.py world
hello world
$  pyinstaller --clean -y -F hello.py
   ...normal INFO messages...
$ dist/hello world
hello world
$  pyinstaller --clean -y -D hello.py
  ... normal INFO messages down to...
3576 INFO: Building COLLECT because out00-COLLECT.toc is non existent
Traceback (most recent call last):
  File "/Developer/VENV-3.6/bin/pyinstaller", line 11, in <module>
    load_entry_point('PyInstaller==3.3.1', 'console_scripts', 'pyinstaller')()
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/__main__.py", line 94, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/__main__.py", line 46, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 791, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/building/build_main.py", line 737, in build
    exec(text, spec_namespace)
  File "<string>", line 33, in <module>
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/building/api.py", line 661, in __init__
    self.__postinit__()
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/building/datastruct.py", line 161, in __postinit__
    self.assemble()
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/building/api.py", line 674, in assemble
    _make_clean_directory(self.name)
  File "/Developer/VENV-3.6/lib/python3.6/site-packages/PyInstaller/building/utils.py", line 388, in _make_clean_directory
    os.makedirs(path)
  File "/usr/local/Cellar/python3/3.6.2/bin/../Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/Developer/VENV-3.6/dist/hello'
~~~~



Discussion:
It seems to have been a long time since this issue popped up, and this still happens. I cannot freeze any program into a onedir now, I am stuck with onefile.
@melvyn2 A pull-request for this issue is still welcome :-)
All you have to change is one line: in [PyInstaller/building/utils.py, line 407](https://github.com/pyinstaller/pyinstaller/blob/develop/PyInstaller/building/utils.py#L407): `if os.path.isdir(path):` to `if os.path.isdir(path) or os.path.isfile(path):`

Resolving patch:
diff --git a/PyInstaller/building/utils.py b/PyInstaller/building/utils.py
--- a/PyInstaller/building/utils.py
+++ b/PyInstaller/building/utils.py
@@ -404,7 +404,7 @@ def _make_clean_directory(path):
     Create a clean directory from the given directory name
     """
     if _check_path_overlap(path):
-        if os.path.isdir(path):
+        if os.path.isdir(path) or os.path.isfile(path):
             try:
                 os.remove(path)
             except OSError:


Test patch: