|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
最近从手机网盘(速度快点)下载了一些美剧来看,但是有一个问题,它下载下来是存在对应的文件夹里面的。因为分类比较多,然后你要找到下载的文件得一层层的剥开文件夹的衣服,然后才能看到你下载的文件。有时打开的是空文件夹,又得重新找,然后....这个脚本就出生了。
主要功能:
将指定文件夹下所有文件(目前还没写过滤,在考虑中)转移到指定文件夹,注意是所有文件,包含子文件夹下的文件,如果 指定文件夹 有 相同文件名的文件,则将当前文件重命名(在文件名后加上数字),已经亲测有效。
下面是代码:
- """
- This script was designed for who need to move all file to one folder.
- For example:
- if u download file from ur wangpan but there are many folders ,
- and u need to cut Then one by one ,now this script can help u.
- """
- import os
- import shutil
- def func(directory= 'D:\\test1', destine_rec ="d:\\test2" ):
- if not os.path.exists(directory): # check if exists
- print ('No such dirctory!')
- if not os.path.isdir(directory): # check if a dir
- print ('try again!')
- # dir_path, dir_name, file_name ---> path , folder, file
- for dir_path, dir_name, file_name in os.walk(directory):
- if not file_name:
- print('There was no file in Current folder, now continue......')
- count = 1
- for file in file_name:
- if os.path.exists(os.path.join(destine_rec, file)): # when file exist,change name
-
- check = True
- while check:
- name_start, name_end = os.path.splitext(file) # a.exe --> a, .exe
- new_file = name_start + str(count) + name_end # a.exe --> a1.exe
-
- if not os.path.exists(os.path.join(destine_rec, new_file)): # destination file doesn't exist
- check = False
- count +=1
-
- else: # file doesn't exist
- new_file = file
-
- os.rename(os.path.join(dir_path, file), os.path.join(dir_path, new_file)) # add path,rename
-
- new_dir = os.path.join(dir_path, new_file) # new path
- shutil.move(new_dir, destine_rec)
- print('Ok ,finished...')
- destine_rec = input('please input destine_folder("eg:d:\\test2"):')
- current_rec = input('please input current folder:("eg:D:\\test1"):')
- func(current_rec, destine_rec)
复制代码
脚本虽小,却能省我不少事!用完感觉还是蛮爽的,满满的成就感!。 |
评分
-
参与人数 2 | 荣誉 +10 |
鱼币 +10 |
贡献 +10 |
收起
理由
|
~风介~
| + 5 |
+ 5 |
+ 5 |
感谢楼主无私奉献! |
冬雪雪冬
| + 5 |
+ 5 |
+ 5 |
热爱鱼C^_^ |
查看全部评分
|