A
A
apiwi2020-04-21 11:49:40
Python
apiwi, 2020-04-21 11:49:40

How to path in Python 3.8.2 to C:\Users\"USERNAME"\Documents?

I have this code:

import urllib.request
print('ShelbyFM: radio station installation...')
url = 'http://d.zaix.ru/iTUp.mp3'  
urllib.request.urlretrieve(url, 'C:\\Users\\USERNAME\\Documents\\Rockstar Games\\GTA V\\User Music\\gta.mp3')


How to make the username (USERNAME) change automatically? Tried %username%, %user% and many other things, doesn't help. This program should download music from the link to this folder.
Thanks, I hope you can help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Pankov, 2020-04-21
@apiwi

import os
os.path.expanduser('~/my/path/from/home')

But it's better to use pathlib
from pathlib import Path
p = Path('~/my/folder').expanduser()  # PosixPath('/home/svp/my/folder')

Of course you will not have posix
from pathlib import Path
p = Path('~/Documents/Rockstar Games/GTA V/User Music/gta.mp3').expanduser()

D
Dr. Bacon, 2020-04-21
@bacon

Ok google: "python user home directory"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question