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

About this user

Tvrtko remind-nix.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Code 2 HTML

This is a simple python program that reads a file (code), and replaces line breaks and spaces with appropriate tag and entity.

   1  
   2  import sys
   3  
   4  line_break='<br>'
   5  nb_space='&#160;'
   6  
   7  def toHtml(f,out):
   8      try:
   9          fo=open(out,'w')
  10      except IOError:
  11          print 'output file error!'
  12      else:
  13          for line in f:
  14              line=line.replace('\t',nb_space*8)
  15              line=line.replace(' ',nb_space)
  16              line=line.replace('\n',line_break)
  17              fo.write(line)
  18          fo.close()
  19          
  20  
  21  if len(sys.argv) == 1:
  22      print("at least one argument / input filename / required")
  23      sys.exit()
  24  if len(sys.argv) > 3:
  25      print"too many arguments"
  26      sys.exit()       
  27  try:
  28      f=open(sys.argv[1],'r')
  29  except IOError:
  30      print 'cannot open file ', sys.argv[1]
  31  else:
  32      if len(sys.argv) == 2:
  33          out=f.name + '.html'
  34      else:
  35          out=sys.argv[2]
  36      toHtml(f, out)
  37      f.close()

decompressing various archive types

// decompressing various archive types with this python script
// usage unpack <archive filename>

   1  
   2  #!/usr/bin/env python
   3  #
   4  # simple python script for extracting mostly used types of archives
   5  # this script extracts .tar, .tar.gz, .tar.bz2, .gz and .zip archives 
   6  #
   7  
   8  import sys	# required for fetching command line arguments 
   9  import os	# required for calling commands for archive extracting
  10  
  11  def unpack(s):									# this is definition of depack
  12  	if (s.find('.tar.gz') != -1):				#	function. It takes string
  13  		os.system("tar -xvvzf " + filename)		#   filename as argument.
  14  	elif (s.find('.tar.bz2') != -1):			#	functon than calls 
  15  		os.system("tar -xvvjf " + filename)		#   appropriate command according
  16  	elif (s.find('.tar') != -1):				# 	to file extension
  17  		os.system("tar -xvvf " + filename)
  18  	elif (s.find('.gz') != -1):
  19  		os.system("gunzip" + filename)			
  20  	elif (s.find('.zip') != -1):		
  21  		os.system("unzip " + filename)
  22  	else: print "Wrong archive or filename"		# other types not supported
  23  
  24  try:											# this is main program
  25  	filename = sys.argv[1]						# first argument right after
  26  	unpack(filename)							#	'unpack' command goes in the
  27  except IndexError:								#	filename string
  28  	print "Filename is invalid!"				#	than the depack function is called
  29  
  30  # try-except block is used for handling IndexError exception if no argument is passed
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS