from sys import executable, argv
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication
import os

def gui_fname(*args,**kwargs):
    directory = args[0]
    if os.path.isfile('.path_profile.txt'):
        with open('.path_profile.txt','r') as f:
            line = f.readlines()
            f.close()
        directory = line[0]

    """Open a file dialog, starting in the given directory, and return
    the chosen filename"""
    # run this exact file in a separate process, and grab the result
    file = check_output([executable, __file__, directory])
    return file.strip()

if __name__ == "__main__":
    directory = argv[1]
    app = QApplication([directory])
    fname = QFileDialog.getOpenFileName(None, "Select a file...", 
            directory, filter="All files (*)")
    with open('.path_profile.txt', 'w', encoding='utf-8') as f:
        f.write(os.path.dirname(fname[0]))
        f.close()    
    print(fname[0])