English 中文(简体)
档案清理过程的问题?
原标题:Problem with file cleaning process?
  • 时间:2011-07-09 21:18:35
  •  标签:
  • python

我正在开发非常庞大的档案系统。 我的任务是用某些参数清理该系统。 下面的方案支离破碎可带来想法。

import DirectoryWalker


extentions_to_delete = list([".rar",".doc",".URL",".js",".EXE",".mht",".css",".txt", ".cache", ".xml"])
extentions_to_copy = list([".jpg",".BMP",".GIF",".jpeg",".gif",".bmp",".png",".JPG"])

dw = DirectoryWalker.DirectoryWalker("/media/08247451247443AA/home/crap/")

def copy_advice(key, files):
    for ext in extentions_to_copy:
        if(ext == key):
            print str(len(files)) + " Files of type " + key + " should be coppied to the target folder."
            for file in files:
                copy_to = "/media/08247451247443AA/home/crap-pics/"

                moved = dw.move_file_to(file, copy_to, True)
                if not moved:
                    print file + " : not moved"

walks = dw.get_all_file_types()


for key in DirectoryWalker.Walk.store.keys():
    files = DirectoryWalker.Walk.store[key]

    copy_advice(key, files)

在<代码>上,以下代码为: 步行是一个简单的类别,有<条码> ,物体。

def get_all_file_types(self):

    extentions = []

    for dirpath,dirnames,filenames in os.walk(self.dir_name):
        for file in filenames:
            extentions.append(Walk(dirpath +"/"+ file))

    return extentions            

def move_file_to(self, file_path, copy_to, rename_if_exists= False):
        file_name = os.path.split(file_path)[1]

        target_file_name = copy_to + file_name;

        coppied = False

        if not os.path.isfile(target_file_name):
            coppied = True
            try:
                os.rename(file_path, target_file_name)
            except OSError:
                coppied = False
                print "Oops! Unable to rename : " + file_path + " to target : " + target_file_name

        if rename_if_exists:
            coppied = True
            file_name = "new_"+ file_name
            try:
                os.rename(file_path, target_file_name)  
            except OSError:
                coppied = False
                print "Oops! Unable to rename : " + file_path + " to target : " + target_file_name        

        return coppied

<代码>Walk

class Walk:

    store = dict([])

    def __init__(self, filename):

        self.file_ext = os.path.splitext(filename)[-1]
        self.file_name = filename

        if not (Walk.store.has_key(self.file_ext)):
            Walk.store[self.file_ext] = list() 

        Walk.store[self.file_ext].append(self.file_name)

但是,当方案执行时,它只能转移近10 400份档案。 但人工计算表明,档案系统中应有13 400份档案。 让我知道,我做了什么错?

A. 最新解决办法

After a careful investigations, I come out with result that there are many ambiguous file names in the target file system and those files were missing.

最佳回答

After a careful investigations, I come out with result that there are many ambiguous file names in the target file system and those files were missing.

问题回答

回答你的问题,为什么不首先采用更简单的法典来测试?

import os

all_files = []

for root, dirs, files in os.walk( /media/08247451247443AA/home/crap/ ):
    all_files.extend(files)

print len(all_files)

作为附带说明,请将“步行”改为defaultdict?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签