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

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 测试完成

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

时间: 2024-08-05 07:05:37

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

通过文件路径读取CSV表格内的数据

ReadDataFromCSV.h UCLASS() class MYPROJECT_API UReadDataFromCSV : public UBlueprintFunctionLibrary { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category = "CSV") static TArray<FString> GetCSVDataToString(FString csvPath); }; Rea

python文件路径读取方法整理(path)

使用Python时,经常用到文件的路径读取,做了path方法的不同读取方式整理: import os #os.path.dirname(__file__)返回的是.py文件的目录 path1=os.path.dirname(__file__) print(path1) #os.path.abspath(__file__)返回的是.py文件的绝对路径(完整路径) path2=os.path.abspath(__file__) print(path2) #组合使用 path3=os.path.dir

Yii2:避免文件路径暴漏,代理访问文件

制作背景:公司要做第三方文件管理系统,客户有时候需要直接访问文件,但是我们又不想暴露文件路径,才有这代理访问 基本功能介绍:读取txt文档.读取图片,如果有需要,可以通过插件读取doc.pdf文档, http://www.yii2.com/uploads/temp/read.bmp是我的真实路径 控制器 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/11/24 0024 * Time: 14:38 */ n

linux 如何获取当前文件路径(source如何获取当前文件路径 bash如何获取当前文件路径)

bash  如何获取当前文件的绝对路径 #!/bin/bash export  bashpath=$(cd `dirname $0`;pwd) echo $bashpath source 文件获取当前文件的绝对路径 #!/bin/bash # 下面是输出当前文件的绝对路径 cd $(dirname $BASH_SOURCE) # echo $(pwd)/$(basename $BASH_SOURCE) temp_file=`echo $(pwd)/$(basename $BASH_SOURCE)

iOS 获取沙盒文件路径及 写入/删除 沙盒文件

出于安全考虑,iOS系统的沙盒机制规定每个应用都只能访问当前沙盒目录下面的文件(也有例外,比如系统通讯录能在用户授权的情况下被第三方应用访问),这个规则把iOS系统的封闭性展现的淋漓尽致. 一.沙盒中几个主要的目录 每个沙盒下面都有相似的目录结构,如下图所示(出自苹果官方文档): 每个应用的沙盒目录都是相似的,主要包含图中所示的4个目录: 1.MyApp.app ①存放内容 该目录包含了应用程序本身的数据,包括资源文件和可执行文件等.程序启动以后,会根据需要从该目录中动态加载代码或资源到内存,这

java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~

在工作开发过程中,每次用SVN提交代码全选择的时候,发现会产生很多不需要的文件后缀垃圾文件,感觉挺烦人的,一个一个删太麻烦了,如果产生多种后缀文件时,那删起来多费劲,是吧?想想,就写了一段程序通过递归进行处理,虽然网上有很多这样的代码,但是我感觉都有问题,网上的代码很多都需要拿过来执行一下才知道有没有问题,如果是多层目录结构的时候,会把你的相同文件名称也干掉,而我写的这个即使是需要删除的文件后缀名称跟文件名一样都不会删掉.每次提交之前我都执行一下再提交,这样就不会有哪些不需要的后缀文件了.这个还

JAVA远程下载文件并读取实例方法

简单的文件下载后读取显示,该方法可返回内容的结果集.一般适用于文本文档的下载,以供学习交流. ** * 远程下载文件并读取返回p * @param filePath 文件网络地址 如http://www.baidu.com/1.txt * @return String */ public String DownAndReadFile(String filePath){ String date = new SimpleDateFormat("yyyy-MM-dd").format(new

Java相对路径读取文件

不管你是新手还是老鸟,在程序中读取资源文件总会遇到一些找不到文件的问题,这与Java底层的实现有关,不能算bug,只要方法得当,问题还是可以解决的. 项目的文件夹结构: repathtest ├─src │    └─com │            └─lavasoft │                    ├─test │                    └─res ├─doc 1.在Java开发工具的project中使用相对路径 在project中,相对路径的根目录是projec

Silverlight获取选择文件的本地物理路径

最近在做一个大文件断点续传的控件,有试过纯C#的代码来写,但后来发现是在是太不灵活了,于是考虑使用控件.但在控件开发过程中发现,如何获取上传文件的物理路径是个大问题,因为Silverlight不支持获取客户端路径的,不仅是Silverlight,所有的微软上传控件都不支持获取本地物理路径.于是我就各种查,终于发现有种方法可以获取到文件的信息了. 先看一个例子 1 #region 选择文件 2 private void bt_SelectFile_Click(object sender, Rout