python打开目录下特定目录 python指定目录_python q切换指定目录

哈喽大家好,今天我们来制作一款能搜索指定路径的python小程序。下面我们开始吧。

|easygui

安装:

pip install easygui

1、msgBox

用法:

msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None)

示例:

import easygui as gmsg = g.msgbox("Hello Easy GUI")#当然你也可以指定信息参数和标题参数title = g.msgbox(msg="我要学会编程!",title="标题部分",ok_button="加油")

运行结果:

python打开目录下特定目录 python指定目录_目标文件_02

2、ccbox()

用法:

ccbox(msg='Shall I continue?', title=' ', choices=('Continue', 'Cancel'), image=None)

示例:

import sysimport easygui as gif g.ccbox("亲爱的还玩吗?",choices=("还要玩!","算了吧/(ㄒoㄒ)/~~")):    g.msgbox("还是不玩了,快睡觉吧!")else:    sys.exit(0)

运行结果:

python打开目录下特定目录 python指定目录_字符串_03

3、ynbox()

老实说,这个函数和ccbox()功能一模一样。。。。。一下省略好几十字。。。

4、buttombox()

用法:

buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)

示例:

import easygui as gg.buttonbox(msg="你喜欢下面哪种水果?",title="",choices=("西瓜","苹果","草莓"))
import easygui as g
g.buttonbox(msg="你喜欢下面哪种水果?",title="",choices=("西瓜","苹果","草莓"))




运行结果:

python打开目录下特定目录 python指定目录_目标文件_04

5、indexbox()

indexbox(msg='Shall I continue?', title=' ', choices=('Yes', 'No'), image=None)

基本跟上面一样,区别就是当用户选择第一个按钮的时候返回序列号0,选择第二个按钮时候返回序列号1。

6、boolbox()

boolbox(msg='Shall I continue?', title=' ', choices=('Yes', 'No'), image=None)

如果第一个按钮被选中则返回 1,否则返回 0。

 8、choicebox()

choicebox(msg='Pick something.', title=' ', choices=())

按钮组件方便提供用户一个简单的按钮选项,但如果有很多选项,或者选项的内容特别长的话,更好的策略是为它们提供一个可选择的列表。

choicebox() 为用户提供了一个可选择的列表,使用序列(元素或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序。  

 9、mutchoicebox()

multchoicebox(msg='Pick as many items as you like.', title=' ', choices=(), **kwargs)

multchoicebox() 函数也是提供一个可选择的列表,与 choicebox() 不同的是,multchoicebox() 支持用户选择 0 个,1 个或者同时选择多个选项

 
multchoicebox() 函数也是使用序列(元素或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序。

10、enterbox()

enterbox(msg='Enter something.', title=' ', default='', strip=True, image=None, root=None)

enterbox() 为用户提供一个最简单的输入框,返回值为用户输入的字符串。

默认返回的值会自动去除首尾的空格,如果需要保留首尾空格的话请设置参数 strip=False。

11、interbox()

用法:

integerbox(msg='', title=' ', default='', lowerbound=0, upperbound=99, image=None, root=None, **invalidKeywordArguments)

12、mulenterbox()

multenterbox(msg='Fill in values for the fields.', title=' ', fields=(), values=())

13、passwordbox()

passwordbox(msg='Enter your password.', title=' ', default='', image=None, root=None)

passwordbox() 跟 enterbox() 样式一样,不同的是用户输入的内容用"*"显示出来,返回用户输入的字符串:

import easygui as gg.passwordbox(msg="请输入您的密码")user_password = g.passwordbox(msg)print(str(user_password))

python打开目录下特定目录 python指定目录_python打开目录下特定目录_05

14、multpasswordbox()

multpasswordbox(msg='Fill in values for the fields.', title=' ', fields=(), values=()

multpasswordbox() 跟 multenterbox() 使用相同的接口,但当它显示的时候,最后一个输入框显示为密码的形式("*"):

import easygui as gmsg = "请输入用户名和密码"title = "用户登录接口"user_info = []user_info = g.multpasswordbox(msg,title,("用户名","密码"))print(user_info)

python打开目录下特定目录 python指定目录_目标文件_06

|程序思路

1、功能:返回用户输入的文件的绝对路径

2、设计思路:

  • (1)用户输入在哪个盘进行查找
  • (2)遍历此盘文件,若为目标文件则输出
  • (2)无此文件,则输出错误

3、实验代码

#查找某个目录下的目标文件import os       #引入操作系统模块import sys      #用于标准输入输出def search(path,name):for root, dirs, files in os.walk(path):  # path 为根目录if name in dirs or name in files:            flag = 1      #判断是否找到文件            root = str(root)            dirs = str(dirs)return os.path.join(root, dirs)return -1path = input('请输入您要查找哪个盘中的文件(如:D:\\\)')print('请输入您要查找的文件名:')name = sys.stdin.readline().rstrip()  #标准输入,其中rstrip()函数把字符串结尾的空白和回车删除answer = search(path,name)if answer == -1:    print("查无此文件")else:    print(answer)

运行结果:

python打开目录下特定目录 python指定目录_目标文件_07

|代码:

import os       #引入操作系统模块import sys      #用于标准输入输出import easygui as g     #引入图形用户界面def search(path1,name):    Allfiles = []           #创建队列    Allfiles.append(path1)while len(Allfiles) != 0:    #当队列中为空的时候跳出循环        path =Allfiles.pop(0)    #从队列中弹出首个路径if os.path.isdir(path): #判断路径是否为目录            ALLFilePath =os.listdir(path)    #若是目录,遍历将里面所有文件入队for line in ALLFilePath:                newPath =path +"\\"+line   #形成绝对路径                Allfiles.append(newPath)else:   #如果是一个文件,判断是否为目标文件            target = os.path.basename(path)if target == name:return pathreturn -1path = g.enterbox(msg='请输入文件目录(如:D:DEV)')name = g.enterbox(msg='请输入您要查找的文件名:')answer = search(path,name)if answer == -1:    g.msgbox("查无此文件",'查找错误')else:    g.msgbox(answer,'返回路径')

运行结果:

python打开目录下特定目录 python指定目录_文件名_08

python打开目录下特定目录 python指定目录_python q切换指定目录_09

python打开目录下特定目录 python指定目录_字符串_10

运行成功~

|总结

本代码比较短,但要细心,容易出错,尤其是格式(别忘了用[],什么东西都能往这个数组里装!)!!!