-4

So I am trying to make a download tool to download stuff via a link here is a snippet from my code

if download_choice == "14":
        print("Downloading test file to Desktop...")
        myfile14 = requests.get(url14)
        open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content)

I want to make a program that can download stuff via a link. I want it to run on all PCs not only on mine. Here is the error:

Traceback (most recent call last): File "C:\Users\David\Desktop\Windows Download Tool\Python\WDT.py", line 100, in <module> open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content) FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/%userprofile%/desktop/'
Thekingis007
  • 627
  • 2
  • 16
  • @lema How exactly does this solve his question? – Thekingis007 Nov 26 '21 at 07:22
  • @lema no i doesnt. – David_Bailey Nov 26 '21 at 07:25
  • The point is: what is the actual question? What's wrong with this code (in terms of result)? What do you expect? You should be more precise in your question – Christophe Nov 26 '21 at 07:27
  • Provide more information please – ani Nov 26 '21 at 07:30
  • @Christophe I want to make a program that can download stuff via a link. I want it to run on all PCs not only on mine. Here is the error Traceback (most recent call last): File "C:\Users\David\Desktop\Windows Download Tool\Python\WDT.py", line 100, in open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content) FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/%userprofile%/desktop/' – David_Bailey Nov 26 '21 at 07:32
  • @David_Bailey put the error Traceback in your question please – Thekingis007 Nov 26 '21 at 07:36
  • Maybe this post could give you at least a clue: https://stackoverflow.com/questions/4028904/what-is-the-correct-cross-platform-way-to-get-the-home-directory-in-python – Christophe Nov 26 '21 at 07:41
  • `open()` can't replace `%userprofile%` with user name - you have to do it on your own. Or you may try to use `os.path.expanduser('c:/users/%userprofile%/desktop/')`or `os.path.expandvars('c:/users/%userprofile%/desktop/')` for this. I don't use Windows but on Linux `os.path.expanduser('~')` or `os.path.expandvars('$HOME')` gives me path to my folder `/home/furas`. I can also use `os.path.expandvars('$HOME/Desktop')` to get `/home/furas/Desktop` – furas Nov 26 '21 at 08:24
  • open() directly followed by write() is a very bad practice – Lukas Nothhelfer Dec 26 '21 at 03:10

1 Answers1

0

Python does not use %userprofile% to get the username of the executing user.

To achieve this, you need to import the package os and run it's getlogin function. This returns a string with the current username

Example:

import os

username = os.getlogin()
if download_choice == "14":
    print("Downloading test file to Desktop...")
    myfile14 = requests.get(url14)
    open(f'C:/Users/{username}/desktop/', 'wb').write(myfile14.content)

I am using an f-string for opening the file, since it is preferred by PEP-8 (it's just correct code-styling)

Attention: This only works on a windows machine

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nTerior
  • 159
  • 1
  • 9