鱼C论坛

 找回密码
 立即注册
查看: 2977|回复: 3

[技术交流] python 实现批量文件转移

[复制链接]
发表于 2016-3-10 11:51:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最近从手机网盘(速度快点)下载了一些美剧来看,但是有一个问题,它下载下来是存在对应的文件夹里面的。因为分类比较多,然后你要找到下载的文件得一层层的剥开文件夹的衣服,然后才能看到你下载的文件。有时打开的是空文件夹,又得重新找,然后....这个脚本就出生了。

主要功能:
将指定文件夹下所有文件(目前还没写过滤,在考虑中)转移到指定文件夹,注意是所有文件,包含子文件夹下的文件,如果  指定文件夹  有   相同文件名的文件,则将当前文件重命名(在文件名后加上数字),已经亲测有效。

下面是代码:
  1. """
  2. This script was designed for who need to move all file to one folder.
  3. For example:
  4. if u download file from ur wangpan but there are many folders ,
  5. and u need to cut Then one by one ,now this script can help u.

  6. """

  7. import os
  8. import shutil


  9. def func(directory= 'D:\\test1', destine_rec ="d:\\test2" ):
  10.     if not os.path.exists(directory):    # check if exists
  11.         print ('No such dirctory!')

  12.     if not os.path.isdir(directory):    # check if a dir
  13.         print ('try again!')

  14.     # dir_path, dir_name, file_name ---> path , folder, file
  15.     for dir_path,  dir_name, file_name in os.walk(directory):
  16.         if not file_name:
  17.             print('There was no file in Current folder, now continue......')

  18.         count = 1
  19.         for file in file_name:
  20.             if os.path.exists(os.path.join(destine_rec, file)):    # when file  exist,change name
  21.                
  22.                 check = True
  23.                 while check:
  24.                     name_start, name_end = os.path.splitext(file)    # a.exe --> a, .exe
  25.                     new_file = name_start + str(count) + name_end    # a.exe --> a1.exe
  26.                     
  27.                     if not os.path.exists(os.path.join(destine_rec, new_file)):    # destination file  doesn't exist
  28.                         check = False   
  29.                     count +=1
  30.                     
  31.             else:    # file doesn't exist
  32.                 new_file = file
  33.                                          
  34.             os.rename(os.path.join(dir_path, file), os.path.join(dir_path, new_file))    # add path,rename
  35.                
  36.             new_dir = os.path.join(dir_path, new_file)    # new path

  37.             shutil.move(new_dir, destine_rec)

  38.     print('Ok ,finished...')

  39. destine_rec = input('please input destine_folder("eg:d:\\test2"):')
  40. current_rec = input('please input current folder:("eg:D:\\test1"):')
  41. func(current_rec, destine_rec)

复制代码


脚本虽小,却能省我不少事!用完感觉还是蛮爽的,满满的成就感!。

评分

参与人数 2荣誉 +10 鱼币 +10 贡献 +10 收起 理由
~风介~ + 5 + 5 + 5 感谢楼主无私奉献!
冬雪雪冬 + 5 + 5 + 5 热爱鱼C^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-3-10 12:50:44 | 显示全部楼层
学以致用。支持。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-10 20:03:00 | 显示全部楼层

,Tku
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-14 10:51:43 | 显示全部楼层
谢谢,这个脚本也很有用,已收!昨天的问题,也已经解决了,谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 23:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表