1. 程式人生 > >Python_Example_文件路徑讀取返回_文件目錄選擇文件_實現程序

Python_Example_文件路徑讀取返回_文件目錄選擇文件_實現程序

excel 選擇 pro tdi 優化 exc turn word explain

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->>> < %s >‘ % local_var_cwd )
21 # [dir_name, file_name] = os.path.split(local_var_cwd) 22 # print(‘分離files & path:< %s > < %s >‘%(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:< %s >‘% (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(‘分離文件名與後綴:< %s > < %s > ‘%(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 : < %s > ‘% 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 : < %s > % 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 : < %s > % 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 : < attribute_protocol.xlsx > 
12 temp_var_path_string:  D:\Auto_Testing_Pycharm\Protocol_Configs\Configs\attribute_protocol.xlsx 
13 測試完成

Python_Example_文件路徑讀取返回_文件目錄選擇文件_實現程序