尝试复制文件时出现权限被拒绝的错误

时间:2018-08-23 15:42:49

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

我正在尝试将文件从输入框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()

我需要使用另一种方法,还是我只是错误地使用了这种方法? 任何帮助都感激不尽。

1 个答案:

答案 0 :(得分:0)

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

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