1400_SpaceCat

Write and append new list parameter (*args)

Aug 10th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import os
  2.  
  3. def writeListName(fileName,createFunction,functionLocalVar,listName,*args):
  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(createFunction+functionLocalVar+str(var_name(listName)+" = "))
  11.         listName.append((args))
  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 file_delete_bracket:
  24.             file_delete_bracket.seek(-1, os.SEEK_END)
  25.             file_delete_bracket.truncate()
  26.             file_delete_bracket.close()
  27.  
  28.         File = open(fileName,"a")
  29.         File.write("\n\t")
  30.         listName.append((args))
  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()
  38.  
  39. listx = []
  40. location_file = "loc.py"
  41. create_function = "def location():\n\t"
  42. function_local_var = "location."
  43.  
  44. writeListName(location_file,create_function,function_local_var,listx, "Name",2000,1)
Advertisement
Add Comment
Please, Sign In to add comment