Archive
Python cheat sheets
For a list of Python cheat sheets, refer to http://www.cheat-sheets.org/#Python.Some direct links:
- Python 2.5 Reference Card (2 pages)
- Python 2.6 Quick Reference (long)
Ref.: https://ubuntulife.wordpress.com/2011/03/29/python-reference-card-tarjeta-de-referencia/ [in English].
Princess Python
“Princess Python (Zelda DuBois) is a fictional character, a supervillain in the Marvel Comics Universe, most notably as a member of the Circus of Crime. She has no superhuman abilities, but rather relies on her snake charming skills and her 25-foot (7.6 m) pet rock python snake. She has fought several superheroes, ranging from the Avengers to Iron Man. She is also notable as she was the first villainess that Spider-Man has faced. She first appeared in The Amazing Spider-Man #22 (Mar 1965).” (source)
More images here.
Useful Python modules
Static HTML filelist generator
Problem
On our webserver I had some files in a directory that I wanted to browse online. However, the webserver didn’t generate a list of links on these files when I pointed the browser to this directory.
Solution
Idea: write a script that traverses the current directory recursively and prints all files with a link to them. I didn’t need any fancy features so the script can produce a very simple output.
Download link: here. Source code:
#!/usr/bin/env python # index_gen.py import os import os.path import sys class SimpleHtmlFilelistGenerator: # start from this directory base_dir = None def __init__(self, dir): self.base_dir = dir def print_html_header(self): print """<html> <body> <code> """, def print_html_footer(self): print """</code> </body> </html> """, def processDirectory ( self, args, dirname, filenames ): print '<strong>', dirname + '/', '</strong>', '<br>' for filename in sorted(filenames): rel_path = os.path.join(dirname, filename) if rel_path in [sys.argv[0], './index.html']: continue # exclude this generator script and the generated index.html if os.path.isfile(rel_path): href = "<a href=\"%s\">%s</a>" % (rel_path, filename) print ' ' * 4, href, '<br>' def start(self): self.print_html_header() os.path.walk( self.base_dir, self.processDirectory, None ) self.print_html_footer() # class SimpleHtmlFilelistGenerator if __name__ == "__main__": base_dir = '.' if len(sys.argv) > 1: base_dir = sys.argv[1] gen = SimpleHtmlFilelistGenerator(base_dir) gen.start()
Usage:
Simply launch it in the directory where you need the filelist. Redirect the output to index.html
:
./index_gen.py >index.html
Don’t forget to set the rights of index.html
(chmod 644 index.html
).
Demo:
Update (20141202)
This version here works but it’s quite primitive. We made a much better version; check it out here: https://pythonadventures.wordpress.com/2014/12/02/static-html-file-browser-for-dropbox/.
Traversing a directory recursively
Problem
You want to traverse a directory recursively.
Solution #1
#!/usr/bin/env python import os def processDirectory ( args, dirname, filenames ): print dirname for filename in filenames: print " " * 4 + filename base_dir = "." os.path.walk( base_dir, processDirectory, None )
os.path.walk()
works with a callback: processDirectory()
will be called for each directory encountered.
Sample output with base_dir = '/etc'
:
/etc/gimp 2.0 /etc/gimp/2.0 ps-menurc sessionrc unitrc
Solution #2, manual method (update at 20110509)
#!/usr/bin/env python import os import sys symlinks = 0 def skip_symlink(entry): """Symlinks are skipped.""" global symlinks symlinks += 1 print "# skip symlink {0}".format(entry) def process_dir(d, depth): print d, "[DIR]" def process_file(f, depth): if depth > 0: print ' ' * 4, print f def traverse(directory, depth=0): """Traverse directory recursively. Symlinks are skipped.""" #content = [os.path.abspath(os.path.join(directory, x)) for x in os.listdir(directory)] try: content = [os.path.join(directory, x) for x in os.listdir(directory)] except OSError: print >>sys.stderr, "# problem with {0}".format(directory) return dirs = sorted([x for x in content if os.path.isdir(x)]) files = sorted([x for x in content if os.path.isfile(x)]) for d in dirs: if os.path.islink(d): skip_symlink(d) continue # else dir_name = os.path.split(d)[1] process_dir(d, depth) traverse(d, depth + 1) for f in files: if os.path.islink(f): skip_symlink(f) continue # else process_file(f, depth) def main(): """Controller.""" start_dir = '.' traverse(start_dir) print "# skipped symlinks: {0}".format(symlinks) #################### if __name__ == "__main__": main()
Solution #3 (update at 20130705)
import os import sys for root, _, files in os.walk(sys.argv[1]): for f in files: fname = os.path.join(root, f) print fname # Remove *.pyc files, compress images, count lines of code # calculate folder size, check for repeated files, etc. # A lot of nice things can be done here # credits: m_tayseer @reddit