Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total

Is the path for a file an absolute path (i.e. fully qualified)?

absolute-path?: func [file] [file = clean-path file]

dirize-ex - like dirize, but allows use of paths

dirize-ex: func [val] [dirize to file! val]

Manipulate files and directory easily with path.py (on pys60)

I really like Jason Orendorff's path.py.
So, I modify it a bit to use with pys60.
- don't load fnmatch, glob, codecs
- remove some functionalities and some docstrings
- match wildcard with regexp simplisticly
#return fnmatch.fnmatch(self.name, pattern)

# poor man's fnmatch
pattern = pattern.replace('.', '\\.').replace('*','.*').replace('?','.')
return re.match(pattern, self.name)

But most of it works nicely. Please report bugs in comments.
Download it here path.py.

Comparing path.py with os.path

Taken from Simon Willson's blog post.
# with os.path.walk
def delete_backups(arg, dirname, names):
    for name in names:
        if name.endswith('~'):
            os.remove(os.path.join(dirname, name))

os.path.walk(os.environ['HOME'], delete_backups, None)

# with os.path, if (like me) you can never remember how os.path.walk works
def walk_tree_delete_backups(dir):
    for name in os.listdir(dir):
        path = os.path.join(dir, name)
        if os.path.isdir(path):
            walk_tree_delete_backups(path)
        elif name.endswith('~'):
            os.remove(path)

walk_tree_delete_backups(os.environ['HOME'])

# with path
dir = path(os.environ['HOME'])
for f in dir.walk():
    if f.isfile() and f.endswith('~'):
        os.remove(f)

Qualify all files in a block with a given path

    qualify: func [
        "Prepend a qualifier/path to all files in a block."
        files [any-block!]
        path  [file! url! string!]
    ] [
        foreach file files [insert file path]
        files
    ]

Using path module

Here's an example codes using path module.
You first need to download it here: path.py
# make some files excutable
d = path('/usr/home/guido/bin')
for f in d.files('*.py'):
    f.chmod(0755)

# Directory walking - delete Emacs backup files
d = path(os.environ['HOME'])
for f in d.walkfiles('*~'):
    f.remove()

Using os.path is difficult. I can hardly remember how to
use which method. Need to lookup the manual every time.
This path module is much much easier to use.
I've heard it will be included in python 2.5 as well.

python : ensure script is executed in its folder path

could be a good start for a new script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys

if __name__ == "__main__":
    _path = os.path.split(sys.argv[0])[0]
    if _path: os.chdir(_path)

specify python's path

You can set sys.path from command line option.
I use it with Jython
java -Dpython.path="%GUESS_HOME%\src\Lib" -classpath "..."

Canonical path

This function transforms an HTTP request path (ie, $_SERVER['PATH_INFO']) into its canonical form, removing empty path elements and relative path elements (//, /./, /../). It assumes that there is a trailing slash on the path if it's a directory.

function canonical_path($path) {
    $canonical = preg_replace('|/\.?(?=/)|','',$path);
    while (($collapsed = preg_replace('|/[^/]+/\.\./|','/',$canonical,1)) !== $canonical) {
        $canonical = $collapsed;
    }
    $canonical = preg_replace('|^/\.\./|','/',$canonical);
    return $canonical;
}
« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total