python怎样实现 先找到文件夹下的所有文件夹,再把这些文件夹下的文件复制到新的文件夹里
defGetFileList1(FindPath):'''''#获取目录中指定的文件名#>>>FlagStr=['F','EMS','txt']#要求文件名称中包含这些字...
def GetFileList1(FindPath):
'''''
#获取目录中指定的文件名
#>>>FlagStr=['F','EMS','txt'] #要求文件名称中包含这些字符
#>>>FileList=GetFileList(FindPath,FlagStr) #
'''
import os
FileList=[]
FileNames=os.listdir(FindPath)
if (len(FileNames)>0):
for fn in FileNames:
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
return FileList
filelist = GetFileList1('D:\\DCIM')
print filelist
def moveFileto(sourceDir, targetDir):
import shutil
shutil.copy(sourceDir, targetDir)
for name in filelist:
moveFileto(name+"\\*","D:\\tttt") 展开
'''''
#获取目录中指定的文件名
#>>>FlagStr=['F','EMS','txt'] #要求文件名称中包含这些字符
#>>>FileList=GetFileList(FindPath,FlagStr) #
'''
import os
FileList=[]
FileNames=os.listdir(FindPath)
if (len(FileNames)>0):
for fn in FileNames:
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
return FileList
filelist = GetFileList1('D:\\DCIM')
print filelist
def moveFileto(sourceDir, targetDir):
import shutil
shutil.copy(sourceDir, targetDir)
for name in filelist:
moveFileto(name+"\\*","D:\\tttt") 展开
1个回答
展开全部
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import logging
import datetime
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='D:\Scripts\move_file.log',
filemode='a+')
def upload_file(src_path, dst_path):
# 目标目录是否存在,不存在则创建
if not os.path.exists(os.path.dirname(dst_path)):
os.makedirs(os.path.dirname(dst_path))
# 本地文件是否存在,存在则移动到目标目录下
if os.path.exists(src_path):
shutil.move(src_path, dst_path)
def main(path):
count = 0
for root, dirs, files in os.walk(path):
for f in files:
count += 1
local_file_path = os.path.join(root, f)
upload_file(local_file_path, local_file_path.replace("xxx", "zzz"))
logging.info(str(datetime.datetime.now()) + " : " + str(count))
if __name__ == '__main__':
path = r"D:\xxx"
try:
main(path)
except Exception as e:
logging.error(e)
刚好刚写完一个。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询