Welcome, guest | Sign In | My Account | Store | Cart

Here are a few basic fuctions that allow someone to copy a directory (along with its files and sub-directories) and paste it somewhere else. They ignore links and anything else that is not a directory or a file. The data returned from copy_dir() is in a format that paste_dir() can understand. A design note: what you get back from copy_dir() may be different whether or not you have a path separator at the end of the path; paste_dir() makes use of that feature (whether or not you want the first folder to be created). The fuctions can easily be modified to not include sub-directories in the returned package, and errors caused by accessing to files or directories can also be ignored with some simple editting. Also, note the asserts in the functions; these functions are designed to crash hard so that the calling code has to take care of the errors.

Python, 85 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''cap_module.py

The purpose of this module
is to provide functions
for copying and pasting
directories and files.

This is a level 1 module.'''

#=========================
# Level 1 Functions: Files
#=========================

def copy_file(path):
    '''copy_file(string)

    Import the needed functions.
    Assert that the path is a file.
    Return all file data.'''
    from os.path import basename, isfile
    assert isfile(path)
    return (basename(path), file(path, 'rb', 0).read())

def paste_file(file_object, path):
    '''paste_file(tuple, string)

    Import needed functions.
    Assert that the path is a directory.
    Create all file data.'''
    from os.path import isdir, join
    assert isdir(path)
    file(join(path, file_object[0]), 'wb', 0).write(file_object[1])

#===============================
# Level 2 Functions: Directories
#===============================

def copy_dir(path):
    '''copy_dir(string)

    Import needed functions.
    Assert that path is a directory.
    Setup a storage area.
    Write all data to the storage area.
    Return the storage area.'''
    from os import listdir
    from os.path import basename, isdir, isfile, join
    assert isdir(path)
    dir = (basename(path), list())
    for name in listdir(path):
        next_path = join(path, name)
        if isdir(next_path):
            dir[1].append(copy_dir(next_path))
        elif isfile(next_path):
            dir[1].append(copy_file(next_path))
    return dir

def paste_dir(dir_object, path):
    '''paste_dir(tuple, string)

    Import needed functions.
    Assert that the path is a directory.
    Edit the path and create a directory as needed.
    Create all directories and files as needed.'''
    from os import mkdir
    from os.path import isdir, join
    assert isdir(path)
    if dir_object[0] is not '':
        path = join(path, dir_object[0])
        mkdir(path)
    for object in dir_object[1]:
        if type(object[1]) is list:
            paste_dir(object, path)
        else:
            paste_file(object, path)

#================
# CGI: Print File
#================

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

Want to capture a directory with its contents? Then this code is for you. Otherwise, maybe you can invent some other uses for it ...

4 comments

Stephen Chappell (author) 18 years, 6 months ago  # | flag

Function(s) Requested. Does anyone know of a standard format for printing directories, their files, their sub-directories, etc? That is the main this that this module is lacking. Is does not have any functions for displaying the contents of a "real" directory or a "packaged" directory. If there was a standard format, I would love to write the code for it or see the code that someone else might write for this purpose. It is annoying when you have a package and cannot (at a glance) see what its contents are. Example: someone doesn't want to paste a package for some specific reason that could easily be identified if they could only look at what the package contained.

Danny Adair 18 years, 5 months ago  # | flag
Stephen Chappell (author) 18 years, 5 months ago  # | flag

Where is the function? I was looking for a function that would print out the list of files, sub-directories, etc. but could not find it. Do you know what the name of the of function is? All I'm looking for is some inspiration as to how someone would want to view a print-out of this information.

Danny Adair 18 years, 5 months ago  # | flag

Walk? Depends on how you would like to format it. walk() will do "depth-first":

from path import path
for sub in path('.').walk(): print sub