python实现将某类文件复制到特定的目录下
2015-11-21 21:02
666 查看
最近在学习python,用python做了个文件搬动的实现
#coding:utf-8
#-*-codding:cp936-*-
import os
import fnmatch
import shutil
import time
#.decode('utf-8').encode('gbk')是为了让raw_input中显示中文
file_suffix=raw_input("输入文件后缀,例如*.txt:".decode('utf-8').encode('gbk'))
source_file=raw_input("输入源文件目录:".decode('utf-8').encode('gbk'))
target_file=raw_input("输入目标文件目录:".decode('utf-8').encode('gbk'))
if os.path.exists(target_file) == False:
print 'The directory is not exist, It had make a new %s' % target_file
os.makedirs(target_file)
record="record.txt"
file_record=os.path.join(target_file,record)
f_handle=open(file_record,'a')
#利用os.walk遍历该文件目录下所有的目录和文件,通过os.path.join(rt,f)得到目标文件的路径,rt是起始路径,dirs是起始路径下的文件夹(列表),files是起始路径下的文件(列表)
def copy_file_to_targetDir(source_file):
file_num=0
for rt,dirs,files in os.walk(source_file):
for f in files:
if fnmatch.fnmatch(f,file_suffix):
file_dir=os.path.join(rt,f)
f_handle.write(file_dir + '\n')
file_num+=1
print file_dir
shutil.copy(file_dir,target_file)
print "\nIt totally find %d matched files" % file_num
print "\nAll the file directory had been saved in record.txt"
time.sleep(3)
copy_file_to_targetDir(source_file)
f_handle.close()
#coding:utf-8
#-*-codding:cp936-*-
import os
import fnmatch
import shutil
import time
#.decode('utf-8').encode('gbk')是为了让raw_input中显示中文
file_suffix=raw_input("输入文件后缀,例如*.txt:".decode('utf-8').encode('gbk'))
source_file=raw_input("输入源文件目录:".decode('utf-8').encode('gbk'))
target_file=raw_input("输入目标文件目录:".decode('utf-8').encode('gbk'))
if os.path.exists(target_file) == False:
print 'The directory is not exist, It had make a new %s' % target_file
os.makedirs(target_file)
record="record.txt"
file_record=os.path.join(target_file,record)
f_handle=open(file_record,'a')
#利用os.walk遍历该文件目录下所有的目录和文件,通过os.path.join(rt,f)得到目标文件的路径,rt是起始路径,dirs是起始路径下的文件夹(列表),files是起始路径下的文件(列表)
def copy_file_to_targetDir(source_file):
file_num=0
for rt,dirs,files in os.walk(source_file):
for f in files:
if fnmatch.fnmatch(f,file_suffix):
file_dir=os.path.join(rt,f)
f_handle.write(file_dir + '\n')
file_num+=1
print file_dir
shutil.copy(file_dir,target_file)
print "\nIt totally find %d matched files" % file_num
print "\nAll the file directory had been saved in record.txt"
time.sleep(3)
copy_file_to_targetDir(source_file)
f_handle.close()
相关文章推荐
- python入门(2)
- Python正则表达式指南
- Python基础 - from import方法与import方法的区别
- Python抓取糗事百科网页信息以及源码下载
- python 正则表达式
- while练习*登陆系统*大小写转换*None与“”
- python中字符与数字的转换
- Python 编码问题 utf-8 和 unicode
- 机器学习算法代码汇总(Python&R)
- python入门(1)
- python 11期 第四天
- python logging动态变更输出日志文件名
- python引入外部文件
- python学习笔记(9)
- python模块之lib2to3(py2转py3自动化工具)
- python 将一个目录下word转为txt第三版
- Python实现K-means聚类
- python定义类
- python 结巴分词的具体应用
- python 连接mysql