帮我用tkinter,os写一个移动文件的程序,可以用鼠标选择待转移文件的文件夹和指定目标文件夹,直接给代码
时间: 2023-03-14 07:20:45 浏览: 73
使用tkinter和os来编写移动文件的程序,可以通过鼠标选择待转移文件的文件夹和指定目标文件夹,需要的代码如下:import tkinter as tk
import osroot = tk.Tk()# 创建一个文件选择对话框
source_dir = tk.filedialog.askdirectory(parent=root, initialdir="/", title='请选择文件夹')# 创建一个文件夹选择对话框
dest_dir = tk.filedialog.askdirectory(parent=root, initialdir="/", title='请选择文件夹')# 将文件移动到指定的文件夹下
files = os.listdir(source_dir)
for f in files:
source_file = os.path.join(source_dir, f)
dest_file = os.path.join(dest_dir, f)
os.rename(source_file, dest_file)
相关问题
帮我写一个打开文件对话框然后选择图片文件打开并保存到指定文件夹的代码
好的,以下是一个基于Python tkinter的简单示例代码,可以实现打开文件对话框选择图片文件并保存到指定文件夹:
```python
import tkinter as tk
from tkinter import filedialog
from PIL import Image
import os
root = tk.Tk()
root.withdraw()
# 打开文件对话框
file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg")])
# 读取图片文件
img = Image.open(file_path)
# 选择保存的文件夹
save_dir = filedialog.askdirectory()
# 获取图片文件名
file_name = os.path.basename(file_path)
# 拼接保存路径和文件名
save_path = os.path.join(save_dir, file_name)
# 保存图片文件
img.save(save_path)
print("图片文件已保存!")
```
这段代码与之前的代码类似,不同之处在于使用了`filedialog`模块的`askdirectory()`方法来选择保存的文件夹,并使用`os`模块来获取图片文件名和拼接保存路径和文件名。你可以根据自己的需要进行修改和扩展。
tkinter程序选取一个文件夹并获取其中所有TXT文件,以tkinter列表框的方式展示,直接给代码
### 回答1:
在 Python 中使用 tkinter 可以很方便地创建 GUI 程序。如果想要选择一个文件夹并获取其中所有 TXT 文件,并使用 tkinter 列表框展示,可以使用如下代码:
```
import tkinter as tk
import tkinter.filedialog as filedialog
import glob
# 创建主窗口
root = tk.Tk()
# 创建列表框
listbox = tk.Listbox(root)
# 创建选择文件夹按钮
def select_folder():
# 选择文件夹
folder_path = filedialog.askdirectory()
# 获取文件夹中所有 TXT 文件
txt_files = glob.glob(folder_path + '/**/*.txt', recursive=True)
# 清空列表框
listbox.delete(0, tk.END)
# 将文件路径添加到列表框
for file in txt_files:
listbox.insert(tk.END, file)
button = tk.Button(root, text='选择文件夹', command=select_folder)
# 布局
button.pack()
listbox.pack()
# 进入消息循环
root.mainloop()
```
在这段代码中,我们使用了 tkinter 和 tkinter.filedialog 模块,分别用于创建 GUI 程序和选择文件夹。使用 glob 模块可以方便地获取文件夹中所有符合指定条件的文件。然后,我们使用 tkinter 列表框显示文件路径。
运行这段代码后,会出现一个 GUI 窗口,点击“选择文件夹”按钮可以选择文件夹,选择完成后会在列
### 回答2:
以下是一个使用tkinter实现的程序,可以选择一个文件夹并获取其中所有的txt文件,并以tkinter列表框的方式展示。
```python
import tkinter as tk
from tkinter import filedialog
import os
def get_txt_files():
folder_path = filedialog.askdirectory() # 选择文件夹路径
txt_files = [file for file in os.listdir(folder_path) if file.endswith('.txt')] # 获取所有的txt文件
# 创建tkinter窗口和列表框
window = tk.Tk()
listbox = tk.Listbox(window)
# 添加txt文件到列表框中
for file in txt_files:
listbox.insert(tk.END, file)
listbox.pack() # 将列表框展示在窗口中
window.mainloop() # 运行程序
get_txt_files()
```
以上程序首先引入了tkinter库,然后创建了一个函数`get_txt_files`来实现选择文件夹和获取txt文件的功能。在函数中,通过`filedialog.askdirectory()`函数来选择文件夹路径,并使用`os.listdir()`函数获取文件夹中的文件列表。然后使用列表解析式筛选出所有以`.txt`结尾的文件。
接下来,创建了一个tkinter窗口和列表框对象,并通过循环将txt文件添加到列表框中。最后将列表框展示在窗口中,并运行程序。
运行程序后,会弹出一个选择文件夹的对话框,选择一个文件夹后,程序会获取文件夹中的所有txt文件,并将其展示在tkinter的列表框中。
### 回答3:
下面是使用tkinter编写的程序,用于选取一个文件夹并获取其中所有的txt文件,并以tkinter列表框的方式展示。
```python
import tkinter as tk
from tkinter import filedialog
import os
def select_folder():
folder_path = filedialog.askdirectory() # 弹出选择文件夹对话框
files = [f for f in os.listdir(folder_path) if f.endswith('.txt')] # 获取文件夹中所有的txt文件
listbox.delete(0, tk.END) # 清空列表框中的内容
for file in files:
listbox.insert(tk.END, file) # 将txt文件添加到列表框中
root = tk.Tk()
root.title('选择文件夹')
root.geometry('300x200')
frame = tk.Frame(root)
frame.pack(pady=20)
button = tk.Button(frame, text='选择文件夹', command=select_folder)
button.pack()
listbox = tk.Listbox(root)
listbox.pack(pady=10)
root.mainloop()
```
这个程序首先创建一个tkinter窗口,在窗口中放置一个按钮和一个列表框。当点击按钮时,会弹出一个选择文件夹的对话框,选择文件夹后,程序会获取该文件夹中所有的txt文件,并将文件名添加到列表框中进行展示。
相关推荐
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)