Search code examples
pythonpython-2.7globpython-ospython-webbrowser

Python script unable to find locate downloads folder and open file


I am writing some automation code using python 2.7 and packages glob, webbrowser, and os to download links using google chrome. I have no issues downloading the file, but once downloaded, my script spits back that " 'C:/Users/UserName/Downloads*' is not recognized as an internal or external command, operable program, or batch file."

The script is supposed to head to a download link, download a file, locate the most recently added file in the downloads folder, and then open that file. The problem occurs AFTER the file has been downloaded, and the script is having issues opening the file from my downloads folder for some reason. Below is the portion of the script giving me issues. It seems like an incredibly simple problem, but I have no idea why this thing can't find my downloads folder. The program runs without any issues on my main PC, but I am having trouble porting it over to my work PC.

elif choice == 'A' or choice == 'B' or choice == 'C':
    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
    file = ''
    if choice == 'A':
        file = A_file
    if choice == 'B':
        file = B_file
    if choice == 'C':
        file = C_file

    webbrowser.get(chrome_path).open(file)
    time.sleep(7)
    list_of_files = glob.glob('C:/Users/UserName/Downloads*')
    latest_file = max(list_of_files,key=os.path.getctime)
    print 'Single file downloaded to location: '+ latest_file
    print 'Opening file...'+"\n"
    p = Popen(latest_file,shell='True')

Solution

  • glob.glob('C:/Users/UserName/Downloads*') includes C:/Users/UserName/Downloads and nothing else, from my testing. If you want to get all of the files within the Downloads directory:

    list_of_files = glob.glob('C:/Users/Andy/Downloads/*')
    

    Seems to be the best bet for this purpose