Advertisement
1400_SpaceCat

Write and append new list parameter (without *args)

Aug 22nd, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import os
  2.  
  3. def writeit(fileName,listName,functionName,localVariable, loc_name,loc_id,loc_lvl):
  4.    
  5.     var_name = lambda x:[ n for n in globals() if id(globals()[n]) == id(x) ][0]
  6.    
  7.     if ( os.stat(fileName).st_size == 0 ):
  8.         File = open(fileName,"w")
  9.        
  10.         File.write("def "+functionName+"():\n\t"+localVariable+"."+str(var_name(listName))+" = ")
  11.         listName.append((loc_name,loc_id,loc_lvl))
  12.        
  13.         list_str = str(listName)
  14.         index    = list_str.index("]")
  15.         cut_list = list_str[:index]+list_str[index+1:]
  16.         result   = cut_list
  17.        
  18.         File.write(str(result)+"]")
  19.         File.close()
  20.        
  21.     elif ( os.stat(fileName).st_size > 0 ):
  22.    
  23.         with open(fileName, 'rb+') as filehandle:
  24.             filehandle.seek(-1, os.SEEK_END)
  25.             filehandle.truncate()
  26.             filehandle.close()
  27.  
  28.         File = open(fileName,"a")
  29.         File.write("\n\t")
  30.         listName.append((loc_name,loc_id,loc_lvl))
  31.        
  32.         list_str = str(listName)
  33.         list_str = list_str.replace("[","")
  34.         list_str = list_str.replace("]","")
  35.        
  36.         File.write(","+str(list_str)+"]")
  37.         File.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement