I want to add a function in my program that when called , opens a browsing window like ones that open when you click on browse to change directory location in a setup. I want it to let me browse through directories , select a file and when I select it, it has to move that file to a pre-decided location in my system. Is it possible ? And if yes , Which module of python should I study and use .
Asked
Active
Viewed 76 times
-3
-
Which framework are you using? – PCM Sep 27 '21 at 15:27
-
1"shutil" can be helpful, too. – Michael Butscher Sep 27 '21 at 15:27
-
@MichaelButscher, no what he meant was a [file dialog](https://en.wikipedia.org/wiki/File_dialog). Shutil cannot do that. – PCM Sep 27 '21 at 15:28
-
1[tkinter.filedialog](https://docs.python.org/3/library/dialog.html#module-tkinter.filedialog)? – ekhumoro Sep 27 '21 at 15:32
-
@PCM The question consists of two parts: selecting file and moving file. "shutil" can help with the latter. – Michael Butscher Sep 27 '21 at 18:58
1 Answers
1
We can use pre built shutil library in python to do copy and move actions.
import shutil
original = 'Path/To/Folder'
target = 'New/Path'
# Copies Folder
shutil.copy(original, target)
# Moves Folder
shutil.move(original, target)
# Copies Complete Tree
shutil.copytree(original, target)
For getting a path by browsing it in file dialog we use prebuilt library named as tkinter like this:
import tkinter
# Returns the path to file opened
tkinter.filedialog.askopenfile()
tkinter.filedialog.askopenfiles()
# Returns the path to folder opened
tkinter.filedialog.askdirectory()

adityanithariya
- 117
- 11