Stavros Michalovits Stavros Michalovits - 5 months ago 85
Python Question

PermissionError: [WinError 5] Access is denied: 'C:\\users\\#username#' using the getpass module

I am using the youtube_dl module to download youtube videos.

import __future__, youtube_dl, getpass

username = getpass.getuser()
url = input('Url: ')

options = {'outtmpl': 'C:/users/"username"/Desktop/%(title)s-%(id)s.%(ext)s',}

with youtube_dl.YoutubeDL(options) as yt:[url])

But when I execute it, I get the following error:

PermissionError: [WinError 5] Access is denied: 'C:\\users\\#username#'

How can I solve this problem from within the program without having to use cmd to get root privileges? I've already tried:

import admin
if not admin.isUserAdmin():

But I get this error:

ModuleNotFoundError: No module named 'admin'

I've tried

pip install admin

but there doesn't seem to be a pip package/module called 'admin'

Answer Source

This doesn't seem to have anything to do with actual privileges, but rather the path you are trying to write to. You have used getuser, but you haven't put it in the outtmpl string. You probably want to do something like this:

options = {'outtmpl': 'C:/users/{username}/Desktop/%(title)s-%(id)s.%(ext)s'.format(username=username),}

Which will use the actual user name running the program instead of a constant "username" string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download