linux保存python文件_Python脚本:linux上将筛选的文件夹复制到另一个目录,保存目录结构...

import os,shutil

def newDir(dir_path):

if not os.path.exists(dir_path):

os.makedirs(dir_path)

def copydir(where_path,go_path,start_time,end_time):

newDir(go_path)

for brand in os.listdir(where_path):

brand_path = os.path.join(where_path, brand)

#print(‘brand_path‘,brand_path)

for site in os.listdir(brand_path):

site_path = os.path.join(brand_path,site)

#print(‘site_path‘,site_path)

for child in os.listdir(site_path):

file_time = ‘-‘.join(child.split(‘-‘)[2:5])

go_dir = go_path + ‘/‘ + brand + ‘/‘ + site#动态生成子目录

print(file_time[:10])

if file_time[:10] >= start_time and file_time[:10] <= end_time:

child_path = os.path.join(site_path,child)

if not os.path.isdir(child_path):

#print(‘file‘,child_path)

newDir(go_dir)

shutil.copy(child_path,go_dir)

else:

#print(‘dir‘,child_path)

#复制文件夹能够自己生成目录

shutil.copytree(child_path,go_dir+‘/‘+child)

#删除文件名包含2018的文件和文件夹

def deldir(path):

for child in os.listdir(path):

child_path = os.path.join(path,child)

if ‘2018‘ in child:

if not os.path.isdir(child_path):

os.unlink(child_path)

else:

shutil.rmtree(child_path)

if __name__ == "__main__":

copydir(‘/lingtian/static_files/mkcms_dev/memo/memo_htmls‘,‘/lingtian/static_files/mkcms_dev‘,‘2018-09-01‘,‘2018-09-30‘)

#deldir(‘/lingtian/static_files/mkcms_dev‘)

我要复制的目录三层,所以有三层循环,保留了原来的目录结构

原文:https://www.cnblogs.com/lelexiu/p/10216837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值