python选择文件并输出路径 |
您所在的位置:网站首页 › gg修改器脚本导入未找到文件 › python选择文件并输出路径 |
1 __all__ = ['Function_Select_File']2 3 #coding=utf-8 4 importos5 6 ''' 7 # ============================================================================8 # Function: 文件路径 函数9 # Explain : 输入参数 无10 # : 输出参数 global_var_file_name_file11 # ============================================================================12 ''' 13 defFunction_Select_File():14 #print('Function_Select_File:') 15 #------------------------------------------------------------------------ 16 globalglobal_var_file_name_file17 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 inrange(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 inlocal_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 =True64 65 whileprotocol_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 =False73 global_var_file_name_file =temp_var_path_string74 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 =False80 global_var_file_name_file =temp_var_path_string81 82 else:83 print('请选择可以执行的协议!')84 protocol_num =True85 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('测试完成') |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |