YouTube不允许我写入我有权访问的文件夹

2024-09-28 22:21:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用youtube-dl制作一个Youtube下载程序,当我指定一个路径时,它会说我没有权限,即使我有权限

以下是脚本:

import tkinter as tk
from tkinter import Tk
from tkinter import messagebox
from tkinter.constants import DISABLED
from tkinter import filedialog
import youtube_dl
import ctypes, sys
window = tk.Tk()
optionswindow = Tk()
f = False
t = True

def closeoptions():
    optionswindow.withdraw()  

def options():
    optionswindow.deiconify()

optionswindow.geometry("360x640")
optionswindow.resizable(t, t)
global canvas2
canvas2 = tk.Canvas(optionswindow, width = 360, height = 640)
canvas2.pack()     
canvas2.create_window(360, 640)

optionswindow.title("Options...")
global pathnotify
pathnotify = tk.Label(optionswindow, text="Where Shall We Save The File?")
pathnotify.place(x=25, y=5)
closebutton = tk.Button(optionswindow, text="Close", width=10, command=closeoptions)
def BrowseLocal():
    Browsed = t
    global TheEntry
    TheEntry = filedialog.askdirectory(title="Where Shall We Save The File?")
    if Browsed == t:  
        getinfo()

def getinfo():
    print(TheEntry)
BrowseButton = tk.Button(optionswindow, text="Browse...", width=40, command=BrowseLocal)
BrowseButton.place(x=25, y=25)
closebutton.place(x=5, y=610)
optionswindow.protocol("WM_DELETE_WINDOW", closeoptions)  

def click():
    messagebox.showinfo("We are now downloading this video","We are now downloading the video, please 
    wait. If you have not specified the path you would like to save the file in, the file will save 
    in your user folder")
    options = {'outtmpl': TheEntry}
    x1 = linkbox.get()
    youtube_dl.YoutubeDL().extract_info(x1, download=False)
    with youtube_dl.YoutubeDL(options) as download:
        download.download([x1])


 optionswindow.withdraw()
 window.title("Youtube Downloader")
 window.minsize(250,50) 
 window.resizable(False, False)
 canvas1 = tk.Canvas(window, width = 275, height = 75)
 canvas1.pack()
 linkbox = tk.Entry (window) 
 canvas1.create_window(100, 25, window=linkbox)
 window.protocol("WM_DELETE_WINDOW", exit)
 optionbutton = tk.Button(window, text="Options...", width=40, command=options)
 optionbutton.place(x=0, y=50)



 downloadbutton = tk.Button(window, compound=tk.LEFT, width=10, height=1,
      text="Download", bg='white', command=click)



 downloadbutton.place(x=170, y=12) 



 window.mainloop() 

我让脚本将视频保存到我的文档中,但它会这样说

ERROR: unable to open for writing: [Errno 13] Permission denied: 'C:\\Users\\Naeem\\Documents'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\downloader\http.py", line 260, in download
    ctx.tmpfilename, ctx.open_mode)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\utils.py", line 2054, in sanitize_open
    stream = open(encodeFilename(filename), open_mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Naeem\\Documents'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\Naeem\Documents\Coding Project\Downloader\Ui\Files\UI.py", line 51, in click
    download.download([x1])
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 2029, in download
    url, force_generic_extractor=self.params.get('force_generic_extractor', False))
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 796, in extract_info
    return self.__extract_info(url, ie, download, extra_info, process)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 803, in wrapper
    return func(self, *args, **kwargs)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 835, in __extract_info
    return self.process_ie_result(ie_result, download, extra_info)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 869, in process_ie_result
    return self.process_video_result(ie_result, download=download)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 1654, in process_video_result
    self.process_info(new_info)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 1936, in process_info
    success = dl(filename, info_dict)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 1875, in dl
    return fd.download(name, info)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\downloader\common.py", line 366, in download
    return self.real_download(filename, info_dict)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\downloader\http.py", line 352, in real_download
    return download()
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\downloader\http.py", line 265, in download
    self.report_error('unable to open for writing: %s' % str(err))
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\downloader\common.py", line 165, in report_error
    self.ydl.report_error(*args, **kargs)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 625, in report_error
    self.trouble(error_message, tb)
  File "C:\Users\Naeem\AppData\Local\Programs\Python\Python37\lib\site-packages\youtube_dl\YoutubeDL.py", line 595, in trouble
    raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: unable to open for writing: [Errno 13] Permission denied: 'C:\\Users\\Naeem\\Documents'

编辑

我现在正在使用askdirectory()并更改youtube dl上的一些选项,现在它可以工作了!感谢@acw1668!显然,我必须使用forcefilname和其他命令来确保它不会覆盖整个目录


Tags: inpyyoutubedownloadliblocallinesite