利用Python做工具-2
来源:互联网 发布:ipad如何进入淘宝闲鱼 编辑:程序博客网 时间:2024/06/11 13:50
前面用Python做了中文提取工具,现在再做一个复制文件的小工具,比较记录如下:
需求描述:
要求再一个固定的文件夹下不断的生成一些固定文件名的文件,供主程序去操作,给主程序进行压力测试。
需求解析:
最简单的就是把一个源文件夹(srcfile)中的不断的移动到目标文件夹(dstfile)
解决方案:
为了尽可能的简洁通用,这里还是使用配置文件作为输入变量的依据。不多说,上代码:
配置文件:config_copy.ini如下:
Python实现代码如下:
Python Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
#!/usr/bin/python
# filename: copyfile.py
# codedtime:2015-5-6
import os
import shutil
import configparser
def excute():
iniconf = configparser.ConfigParser()
iniconf.read('config_copy.ini')
sourceDir = iniconf.get('setting', 'sourceDir')
targetDir = iniconf.get('setting', 'targetDir')
while True:
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if not os.path.exists(targetFile):
shutil.copyfile(sourceFile, targetFile)
if __name__ == '__main__':
excute()
#!/usr/bin/python
# filename: copyfile.py
# codedtime:2015-5-6
import os
import shutil
import configparser
def excute():
iniconf = configparser.ConfigParser()
iniconf.read('config_copy.ini')
sourceDir = iniconf.get('setting', 'sourceDir')
targetDir = iniconf.get('setting', 'targetDir')
while True:
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if not os.path.exists(targetFile):
shutil.copyfile(sourceFile, targetFile)
if __name__ == '__main__':
excute()
心得体会:
1、python之所以效率高,就在于有那么多现成的模块如: shutil、configparser等供你去使用,自己只要动手组织起来就可以了。
2、C++几个小时搞不定的东西,Python可能只需要几分钟就搞定,做一些小工具,着实方便,可爱的Python!!!
值得注意的地方:
1、 shutil.copyfile抛出异常:
异常分析:
解决办法:
0 0
- 利用Python做工具-2
- 利用Python做工具-1
- 利用Python做工具-3
- 使用UnityEditor做工具
- python做工具遇到报错:[Errno 22] invalid mode ('rb') or filename
- 自己做工具--封装一个DAO_PDO类
- 自己做工具--用原生js封装一个AJAX插件
- Camera360每天新增15-20万用户 坚持做工具不涉足社区
- whatsApp 被收购随想: 做工具累积用户,跟巨头合作实现平台收益
- 自己做工具--封装PHP的PDO类--单例模式
- Python 小工具集合
- [python] 用户名小工具
- python小工具
- Python 迭代工具
- Python 内置小工具
- python 小工具总结
- Python迭代工具
- python迭代工具
- EXT.NET 按钮调后台方法之前做验证的方法
- Perl 清空文件c1
- Dialogue System for Unity制作剧情,我们需要一个什么样的需求
- MyISAM InnoDB 区别
- 切图总结
- 利用Python做工具-2
- android开发之android:process属性_application中onCreate执行多次
- 重回CSDN
- Java集合框架——迭代器
- excel中分开的度分秒合并到一个单元格以及转换成相应的度
- 多线程还是多进程的选择及区别
- oracle10g/11g 新特性六(数据库维护)
- 如何利用抽象工厂更换数据库
- OCday1 类和对象