我有一个程序,我正在尝试写一个需要一个非常大的目录(里面有10,000多个文件),并且会创建新的子目录以将非常大的目录分割成更小的块(每个大约100个文件)。当我在终端中调用它时,我目前没有提出任何错误,但它实际上没有对大文件进行排序......我认为问题与os.rename()相关,但我不理解为什么我也试过shutil.move()并且仍然有同样的问题。对不起,我couldent使代码出现在颜色我是新来的网站为什么os.rename程序没有排序目录
#!/usr/bin/python
import os
import glob
import sys
from functools import partial
sys.setrecursionlimit(1000)
def mk_osdict(a):
#os.chdir(a)
#grouping files with .mol2 endings only
os_list =glob.glob("*.mol2")
#making a dictionary for the list of files in the directory
os_dict = dict([i,n] for i,n in zip(range(len(os_list)),os_list))
return os_dict
dict_os = mk_osdict("decoys")
#function to sort files into new directories with a specific size.
def init_path(f):
block = (len(f)/100)+1
#i_lst gives a list of the number of entries
i_lst = [str(i) for i in range(block)]
'''paths keys will become new directories, values will be a list
files to be sorted into the corresponding directory'''
paths = dict(["decoydir"+n.zfill(5),[]] for n in i_lst)
for lst in paths.values():
while len(lst) <= block:
for value in f.values():
lst.append(value)
for x,p in paths:
if not os.path.exists(x):
os.mkdir(x)
else:
pass
for index in p:
yield os.rename(index,os.path.join(x,index))
b = init_path(dict_os)
你提到“,但它实际上并没有排序的大型文件“;你的意思是'它不会从大目录中删除文件'?重命名不排序;它重命名文件,并且重命名的副作用可能是将单个文件从一个(大)目录移动到一个(新的,小的)目录中。如果您正在忙于在进程正在扫描时更改目录的内容,但是它可能不会中断,我还没有探讨过会发生什么情况。 – 2015-04-01 16:06:17
这不会有几个原因,但是你的问题的一部分是'init_path'使用'yield'语句,它使得它成为一个生成器。所以,只需调用它'b = init_path(dict_os)'(这也不起作用,因为需要两个参数)只是简单地初始化生成器并且不执行任何重命名。 – tdelaney 2015-04-01 16:10:08
@JonathanLeffler对此感到抱歉。我的意思是,当我运行程序时,没有文件被分类到创建的新目录中。 – 2015-04-01 18:09:52