【问题标题】:Getting a Permission Denied Error When Trying to Copy Files尝试复制文件时出现权限被拒绝错误
【发布时间】:2019-01-30 01:21:26
【问题描述】:

我正在尝试将文件从输入框 1 复制到输入框 2 的位置。我收到一个错误:

File "File_Name\Shutil.py", line 121, in copyfile 
with open (dst, 'wb') as fdst:
PermissionError: [Errno 131] Permission Denied: "File_Path"

这是我要传递的内容:

def copy_command():
    copyfile(e1.get(),e2.get())

源代码如下:

import tkinter
import threading
import os
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter.filedialog import *
from pathlib import Path
#from shutil import copyfile
from shutil import *


def open_file_01():
    name=askopenfilename(initialdir="C:/Users/%User%/Documents", filetypes=(("Text Files","*.txt"),("Data Files","*.dbs"),
                                                                            ("PNG Files","*.png"),("JPEG Files","*.jpg"),
                                                                            ("PDF Files","*.pdf"),("ISO Files","*.iso"),
                                                                            ("DOCX Files","*.docx"),("Exel Files","*.xlsx"),
                                                                            ("CSV Files","*.csv"),("PowerPoint","*.pptx"),
                                                                            ("ZIP Files","*.zip"),("All Files","*.*")))
    e1.delete(0,END)
    e1.insert(0,name)

def open_file_02():
    dir=askdirectory()
    e2.delete(0, END)
    e2.insert(0,dir)

def copy_command():
    copyfile(e1.get(),e2.get())

main=Tk()
main.configure(background='grey95')
main.title("temporary")
width = 500
height = 175
screen_width = main.winfo_screenwidth()
screen_height = main.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
main.geometry("%dx%d+%d+%d" % (width, height, x, y))
main.resizable(0, 0)

#File Menu
menu = Menu(main)
new_item = Menu(menu, tearoff=0)
new_item.add_command(label='Quit', command=main.destroy)
menu.add_cascade(label='File', menu=new_item)
main.config(menu=menu)

#Schedual
menu2 = Menu(main)
new_item = Menu(menu, tearoff=0)
menu.add_command(label='Scheduale')
main.config(menu=menu)

l1=Label(main, text="From:")
l1.grid(row=1, column=0, pady=(20,6))
l2=Label(main, text="To:")
l2.grid(row=2, column=0, pady=6)

from_dir=StringVar()
e1=Entry(main, width=50, textvariable=from_dir)
e1.grid(row=1, column=1, pady=(20,6))
to_dir=StringVar()
e2=Entry(main, width=50, textvariable=to_dir)
e2.grid(row=2, column=1, pady=6)

b1=Button(main, text="Browse...", width=10, command=open_file_01)
b1.grid(row=1, column=2, columnspan=6, pady=(20,6))
b2=Button(main, text="Browse...", width=10, command=open_file_02)
b2.grid(row=2, column=2, columnspan=6, pady=6)

b3=Button(main, text="Start", width=10, command=copy_command)
b3.grid(row=3, column=0, padx=3, pady=6)

pb=ttk.Progressbar(main, orient=HORIZONTAL, length=400, value=100)
pb.grid(row=3, column=1, columnspan=2)

b4=Button(main, text="Cancel", width=10)
b4.grid(row=4, column=0, columnspan=3, padx=2, pady=6)

main.mainloop()

我需要使用不同的方法,还是我只是使用了错误的方法? 任何帮助都感激不尽。

【问题讨论】:

  • 底线正是错误告诉您的内容:您无权做您想做的事。这可能就是这个意思,也可能意味着您将文件复制到与您想象的不同的位置。您是否进行了任何调试以验证 e1.get()e2.get() 是否返回了您认为它们返回的内容?您是否考虑到路径可能相对于错误的起点?
  • 我知道权限被拒绝。我是我自己机器上的管理员。除了具有无效的文件路径(这是有效的)之外,我还能做些什么来赋予程序复制文件的权限?还是有更好的方法来完成这项任务?
  • 如果您确定文件路径正确,则需要更改您尝试复制文件的文件夹的权限。
  • 看起来您已将文件放在管理员路径中。尝试将其放在任何其他文件夹中,并且不要忘记更改代码中的路径。
  • 每个文件夹都具有适当的权限,不应阻止传输。文我做了一个print(e1.get()) 和一个print(e2.get()),它返回我指定的实际路径。

标签: python-3.x tkinter shutil file-copying


【解决方案1】:

我不得不将它从 e1 和 e2 更改为 from_dir 和 to_dir,我还必须摆脱 shutil.copyfile 前面的 shutil

def copy_command(buffer_size=16000):
        src=from_dir.get()
        dest=to_dir.get()
        copy(src, dest)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-09
    • 2019-04-28
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2019-12-29
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多