Python implements dialog box to select folder or file (update 1, add detailed usage of filedialog dialog box)

The function of python for selecting files or folders is in the tkinter module, which can be realized with the following code:

#! python3

import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()

FolderName = filedialog.askdirectory()  #获取文件夹
FileName = filedialog.askopenfilename()  #获取文件夹中的某文件

if '/' in FolderName :

    # 用\替换/,注意'\\'的用法,
    # 如果直接使用'\',会被系统识别成转义字符
    FolderName.replace('/', '\\') 
    print('找到1')

if '/' in FileName :
    FileName.replace('/', '\\')
    print('找到2')

if len(FolderName) == 0 :
    print('未找到文件夹!')
else :
    print('文件夹是:',FolderName)

if len(FileName) == 0 :
    print('未找到文件夹!')
else :
    print('文件是:',FileName)

In the above code, the following two lines of code are used:

import tkinter as tk
from tkinter import filedialog

It is said that all members in the module have been imported using the import tkinter statement, why do we have to use the from tkinter import filedialog statement?

Please see the link: https://editor.csdn.net/md/?articleId=128321249

The method and parameters of the filedialog dialog box in python:
tkinter.filedialog.asksaveasfilename(**options): Select the filename to save the currently opened file, and return the filename string containing the full path. You need to select an existing file, and you can only select one file, or you can enter a new file name to save it as a new file.
tkinter.filedialog.asksaveasfile(**options): Choose what file to save, create the file and return the file stream object (I don’t understand file stream yet, so I list it here temporarily, return value example: <_io.TextIOWrapper name='C :/Users/gsl/Desktop/Today's homework/polyphonic 1 word.docx' mode='w' encoding='cp936'>)
tkinter.filedialog.askopenfile(**options): Choose what file to open and return an IO stream object (Same as above, return value example: <_io.TextIOWrapper name='C:/Users/Gao Shulin/Desktop/Today's homework/multi-tone 1 word.docx' mode='r' encoding='cp936'>)
tkinter.filedialog.askopenfilename (**options): Select a single file to open, and return the file name string containing the full path
tkinter.filedialog.askopenfilenames(**options): Select multiple files to open, return the selected file in tuple form The file name string tuple of the full path, or only one file can be selected
tkinter.filedialog.askdirectory(**options): Select a folder and return the folder name containing the full path
tkinter.filedialog.askopenfiles(**options): Choose to open multiple files, return a list of multiple IO file stream objects, or open a file.

Parameters of the above method **options Option introduction:
**optins parameter, usually composed of title, defaultextension, filetypes, initialdir, multiple.
title: Specifies the title of the dialog box, a string. (not required)
defaultextension: Specify the default extension of the file, for example: defaultextension='.jpg', then when the user enters a file name 'Python', the file name will automatically add the suffix 'Python.jpg'. –Note: If the user input file name contains suffix, then this option will not take effect. (optional)
  filetypes: Specify the drop-down menu option for filtering file types. The value of this option is a list of two-tuples. Each two-tuple is composed of (type name, extension), for example: filetypes=[ ('text', '.txt'), ('raster', '.tif'), ('gif', '.gif')]. (Set as needed, when there are many files and many types in the folder, it is recommended to give this parameter), (not required).
  initialdir: Specifies the default path to open or save files, the default path is the current folder. (Optional)
  multiple: Whether to determine whether to select multiple files, if it is True, one or more files can be selected. (optional)

These parameters are not available for every method, use the options as needed.

The above content refers to the articles of the following bloggers, and I would like to express my thanks.
Copyright statement: This article is an original article of CSDN blogger "Unnamed Programming", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: https://blog.csdn.net/qq_44275213/article/details/106388710Copyright
statement: This article is the original article of CSDN blogger "Sinte", following the CC 4.0 BY-SA copyright agreement, please attach the original text for reprinting Source link and this statement.
Original link: https://blog.csdn.net/weixin_44630029/article/details/104399156

Guess you like

Origin blog.csdn.net/any1where/article/details/128276355