将所选文件复制到python中的目录

时间:2019-01-30 15:13:28

标签: python tkinter shutil

我正在尝试将使用对话框窗口选择的文件复制到相对目录。

from tkinter import filedialog
from tkinter import *
from shutil import copy2
import os


root = Tk()
root.withdraw()
source = filedialog.askopenfilename(
    initialdir="/", title="Select file", filetypes=(("HTML Files","*.html"), ("all files", "*.*")))
target = '/import'
print("Selected file " + source)
copy2(source, target)

选择文件后,它不会出现在目标目录中。

编辑:添加了第二个文件对话框以选择目标目录,现在可以使用了。

from tkinter import filedialog 
from tkinter import *
from shutil import copy2
import time


root = Tk()
root.withdraw()
root.source = filedialog.askopenfilename(initialdir = "/",title = "Select source file",filetypes = (("html files",".html"),("all files",".*"))) 
print ("Selected file " + root.source)
time.sleep(1)
root.target = filedialog.askdirectory(initialdir = "/",title = "Select target directory")
print ("Selected directory " + root.target)
copy2(root.source, root.target, follow_symlinks=True)

1 个答案:

答案 0 :(得分:0)

您应该更仔细地检查文档。您的目标应该是完整的文件目录,而不仅仅是y字符串。在您的示例中,应如下所示:

int CurrentIndex