A
A
Alexey2021-03-14 00:05:07
Python
Alexey, 2021-03-14 00:05:07

Why does the program not see the path to the file?

I have this code

import requests
import sys
import os
def install(package: str):
    request = requests.get("http://api.com/v1.0/package?name="+package)
    package_json = request.json()
    print(request)
    print("Package name: "+package_json["display_name"])
    print("Description: "+package_json["description"])
    print("Package ID: "+str(package_json["id"]))
    downloads = "./downloads/{}.exe".format(package)
    with open(downloads, "wb") as f:
        response = requests.get(package_json["download_url"], stream=True, allow_redirects=True)
        total = response.headers.get('content-length')

        if total is None:
            f.write(response.content)
        else:
            downloaded = 0
            total = int(total)
            for data in response.iter_content(chunk_size=max(int(total / 1000), 1024 * 1024)):
                downloaded += len(data)
                f.write(data)
                done = int(50 * downloaded / total)
                sys.stdout.write('\rDownloading: [{}{}] {}% Complete'.format('█' * done, '-' * (50 - done), done*2))
                sys.stdout.flush()
    print("\n\nRunning installer executable...")
    os.startfile(os.path.dirname(os.path.abspath(__file__))+"\\downloads\\{}.exe".format(package))


if __name__ == '__main__':
    try:
        strg = str(sys.argv[1])
        print(strg)
        print(sys.argv)
        strg_final = strg.replace(r"client://", "")
        print(strg_final)
        install(strg_final)
    except KeyboardInterrupt:
        print("\ncancled by user")

the fact is that everything works in the console, but when I run the program through the browser, the arguments are transmitted but it constantly knocks out such an error 604d28f0eddff818548503.png. How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
4
4X_Pro, 2021-03-14
@graymstar

You need to form the path in the same way. Otherwise, in the first case, it is written relative to the current directory:
"./downloads/{}.exe".format(package)
and in the second case, relative to the path where the script is located:
os.path.dirname(os.path.abspath (__file__))+"\\downloads\\{}.exe".format(package)
If the current directory is the directory with the script, then everything works fine, if not, then the file cannot be found. And I would use the second option (with an absolute path).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question