ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: z_directory.py
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/07/05
Version no: 1.0
Category: Files

 

Not Rated yet


Description:

This recipe is a memory-based directory copier and paster. Pickled Directory objects will contain all data from a directory at path and can be saved in other locations. Once a Directory object has been created, pasting it to another location is extremely simple.

Source: Text Source

'''Support module for working with directories.

This module provides several functions for
copying and pasting directories and files.'''

__version__ = 1.3

################################################################################

import os

class File:

    'File(path) -> new file'

    def __init__(self, path):
        'Initializes a new file object.'
        self.__name = os.path.basename(path)
        self.__data = file(path, 'rb', 0).read()

    def paste(self, path):
        'Creates a new file at path.'
        file(os.path.join(path, self.__name), 'wb', 0).write(self.__data)

class Directory:

    'Directory(path) -> new directory'

    def __init__(self, path):
        'Initializes a new directory object.'
        self.__name = os.path.basename(path)
        self.__data = list()
        for name in os.listdir(path):
            path_name = os.path.join(path, name)
            if os.path.isdir(path_name):
                self.__data.append(Directory(path_name))
            elif os.path.isfile(path_name):
                self.__data.append(File(path_name))

    def paste(self, path):
        'Creates a new directory at path.'
        if self.__name:
            path = os.path.join(path, self.__name)
            os.mkdir(path)
        for item in self.__data:
            item.paste(path)

################################################################################

if __name__ == '__main__':
    import sys
    print 'Content-Type: text/plain'
    print
    print file(sys.argv[0]).read()

Discussion:

This recipe is effective for pasting small directories to several different locations. Large directories can be copied, but performance drops when all of RAM is occupied. This recipe was mainly an experiment for grabbing a directory, storing it in memory, and then writting it back out to disk.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.