Python_Example_文件路径读取返回_文件目录选择文件_实现程序

2021-06-29 14:04

阅读:570

标签:UNC   ict   keyword   ber   pytho   string   os.path   result   nbsp   

2018-09-14

Author: 楚格

IDE: Pycharm2018.02   Python 3.7   

KeyWord : 文件路径 文件名称选择

Explain:  Testing_Config_List

 思路:

1. def Function_Select_File():  获取当前路径(调试中默认路径)

2. 遍历 当前文件夹下的所有文件和文件夹 以供选择使用

3. 拼接字符串 返回有用的路径

4.着重处理了 文件选择部分  方法还可以继续优化

5. code 一步步搭建,注释部分是初始调试部分,方便错误查找

1------------------------------------------------------------------------------------------------------------------

 

--

 1 __all__ = [Function_Select_File]
 2 
 3 # coding=utf-8
 4 import os
 5 
 6 ‘‘‘
 7 # ============================================================================
 8 # Function:  文件路径  函数
 9 # Explain :  输入参数   无
10 #         :  输出参数  global_var_file_name_file
11 # ============================================================================
12 ‘‘‘
13 def Function_Select_File():
14     # print(‘Function_Select_File:‘)
15     #------------------------------------------------------------------------
16     global  global_var_file_name_file
17 
18     # 获取当前文件路径 调试使用
19     # local_var_cwd = os.getcwd()
20     # print(‘当前文件夹的路径  :\n->>>  ‘ % local_var_cwd )
21     # [dir_name, file_name] = os.path.split(local_var_cwd)
22     # print(‘分离files & path:     ‘%(dir_name,file_name))
23 
24     # 固定文件夹目录
25     local_var_cwd = D:\Auto_Testing_Pycharm\Protocol_Configs\Configs
26     local_var_list_name = os.listdir(local_var_cwd)  # # print(‘当前文件夹下的所有文件和文件夹:‘)
27 
28     #初始化列表 存放文件
29     local_var_num = []
30     local_var_file_name = []
31     local_var_storage = {}
32 
33         # 遍历文件夹 编号 +  文件名
34     for temp_var_num in range(len(local_var_list_name)):
35         # print(‘num:%s   name:‘% (temp_var_num,local_var_list_name[temp_var_num]))    # display All
36         [new_file_name, file_name_end] = os.path.splitext(local_var_list_name[temp_var_num])  # file name handle
37         # print(‘分离文件名与后缀:    ‘%(new_file_name,file_name_end))              # display result
38 
39             # 过滤其他文件 保留Excel文件 以供选择
40         if file_name_end == .xlsx:
41             local_var_num.append(temp_var_num)                                      # 存储文件编号
42             local_var_file_name.append(local_var_list_name[temp_var_num])           # 存储文件名称
43             # print(‘Display select files : ‘, local_var_num, local_var_file_name)  # 显示二个列表
44         #------------------------------------------------------------------------------------------
45 
46 
47         # 显示留存的文件  二个列表映射成字典 保留列表方便调试使用  技巧 ***
48     local_var_storage = dict(zip(local_var_num, local_var_file_name))
49 
50         #  提示 遍历字典元素 显示内容 local_var_storage.items() 无序
51         #  可以这个替换 : print(‘Your selecr pprotocol :  ‘% local_var_storage.keys())
52     print(Protocols that can be executed :  )
53     for temp_var_num in local_var_storage :
54         print("",temp_var_num,local_var_storage[temp_var_num])
55     #-----------------------------------------------------------------------------
56 
57 
58     print(List number :,local_var_num)    # 显示可行选择的文件编号
59     # 后期拓展 使用此做做默认处理
60     print(Please your select file number : \n ->>> )
61     # local_var_select_number = input(‘please your select file number : \n ->>> ‘)
62     local_var_select_number = "0"
63     protocol_num = True
64     
65     while protocol_num:
66 
67         if  local_var_select_number  == "0" :
68             print(Your select protocol :  % local_var_file_name[int(local_var_select_number)])
69                 # 拼接路径
70             temp_var_path_string = local_var_cwd +\\+ local_var_file_name[int(local_var_select_number)]
71             print(temp_var_path_string:  %s % temp_var_path_string)
72             protocol_num = False
73             global_var_file_name_file = temp_var_path_string
74 
75         elif local_var_select_number == "1":
76             print(Your select protocol :   % local_var_file_name[int(local_var_select_number)])
77             temp_var_path_string = local_var_cwd + \\ + local_var_file_name[int(local_var_select_number)]
78             print(temp_var_path_string:  %s  % temp_var_path_string)
79             protocol_num = False
80             global_var_file_name_file = temp_var_path_string
81 
82         else:
83             print(请选择可以执行的协议!)
84             protocol_num = True
85 
86     return global_var_file_name_file  # 出口
87     # -----------------------------------------------------------------------------
88 
89 # ============================================================================
90 
91 ‘‘‘
92 # ============================================================================
93 #   测试专用
94 # ============================================================================
95 ‘‘‘
96 if __name__ == "__main__":
97     print(测试开始)
98     Function_Select_File()
99     print(测试完成)

 

--

run result

--

 

 1 测试开始
 2 Protocols that can be executed :  
 3  0 attribute_protocol.xlsx
 4  1 read_test - 副本.xlsx
 5  2 read_test.xlsx
 6  3 SG80KTL.xlsx
 7  5 z.xlsx
 8 List number : [0, 1, 2, 3, 5]
 9 Please your select file number : 
10  ->>> 
11 Your select protocol :  
12 temp_var_path_string:  D:\Auto_Testing_Pycharm\Protocol_Configs\Configs\attribute_protocol.xlsx 
13 测试完成

 

Python_Example_文件路径读取返回_文件目录选择文件_实现程序

标签:UNC   ict   keyword   ber   pytho   string   os.path   result   nbsp   

原文地址:https://www.cnblogs.com/caochucheng/p/9646393.html


评论


亲,登录后才可以留言!