os.mkdir raises a generic exception if an error occurs, but typically you need to ignore preexisting directory errors. Here's how to ignore them:
import os, errno, sys
myPath = "/path/to/dir"
try:
os.mkdir(myPath)
except Exception, e:
code, st = e
if code != errno.EEXIST:
st = "Error creating directory '%s': %s" % (myPath, str(e))
sys.exit(1)
directoryexceptionslanguagesmkdirosprogrammingpython
To zip up the contents of a directory (selectively), use find and zip:
$ find . -type f -name "*.jpg" | zip -@ myimages.zip
commandsdirectoryfindshellzip
You can use RSS feeds to create live bookmark menus in Firefox. Using the menus, go to Bookmarks -> Manage Bookmarks -> File -> New Live Bookmark. Enter RSS feed information. In your bookmarks menu you'll have a directory-like listing of the RSS information.
bookmarksbrowserdirectoryfeedfirefoxliverssui
If you use pushd and popd to maintain your directory stack, then ~N (where ~0 is the top of the stack) will expand to the appropriate directory in the stack. For example:
cygnus:~$ pushd usr ~/usr ~ cygnus:~/usr$ pushd /etc /etc ~/usr ~ cygnus:/etc$ pushd /usr/lib /usr/lib /etc ~/usr ~ cygnus:/usr/lib$ cd ~2 cygnus:~/usr$
Furthermore, you can make cd use pushd and popd functionality implicitly:
~> DIRSTACKSIZE=8 ~> setopt autopushd pushdminus pushdsilent pushdtohome ~> alias dh='dirs -v' ~> cd /tmp /tmp> cd /usr /usr> cd bin /usr/bin> cd ../pub /usr/pub> dh 0 /usr/pub 1 /usr/bin 2 /usr 3 /tmp 4 ~ /usr/pub> cd -3 /tmp> dh 0 /tmp 1 /usr/pub 2 /usr/bin 3 /usr 4 ~ /tmp> ls =2/df /usr/bin/df /tmp> cd -4 ~>
(Note: The text above is taken essentially verbatim from http://www.b2pi.com/zsh/Intro/intro_6.html.)
directoryextrasstackzsh
Download an entire directory tree:
$ wget -r ftp://username:password@site/path/to/suck
commandsdirectorydownloadftpshelltreewget
Make sure you always specify a path free of symlinks. This can be pretty tough, though. An alternative approach is to use namei to track down symlinks:
# namei /usr/X11/bin/xterm f: /usr/X11/bin/xterm d / d usr l X11 -> X11R6 d X11R6 d bin - xterm
commandsdirectorynameipathshellsymlinkstree
If you have a bunch of files in your home directory and you want to push them into ~/sort, create the directory and then do the following:
$ find . -type f -maxdepth 1 ! -name ".?*" | xargs -I '{}' mv '{}' sort
Note: GNU xargs uses -i. BSD versions use -I.
bsdcommandsdirectoryfindgnushellsortxargs