python 2.7 - Writing between between characters in a text file? -
python 2.7 - Writing between between characters in a text file? -
i have module want write into. i'm having several problems. 1 of locating string within file. open file, utilize line in (filename), if determine if finds string, , of works. before (it commented out now) tried determine position @ using tell(). gave me wrong position, giving me 1118 believe, instead of 660 something. determined position manually utilize seek.
however sec problem was, if write file @ position in file, overwrites info thereon. want insert info instead of overwriting it.
unless insert string equal in character length want write happen, override of if statements , things below.
is there way naively this?
here file want write into
# filename: neo_usercurves.py # created by: gregory smith # description: script containing library of user created curves # purpose: library store names of user curves, , deletes curves # if specified import os import maya.cmds mc import module_locator my_path = module_locator.module_path() def usercurve_lib(fbxfile=none, remove=none): """all control/curve objects created user keyword arguments: fbxfile -- (string) name of fbx file import remove -- (boolean) remove entry library , delete associated fbx file """ curves_dict = { #crvstart #crvend } if remove none: homecoming curves_dict elif not remove: try: name = mc.file(curves_dict[fbxfile], typ='fbx', i=1, iv=true, pmt=false) homecoming name[0] except runtimeerror: homecoming none else: try: os.remove('%s\%s.fbx' %(my_path, fbxfile)) homecoming '%s.fbx' %(fbxfile) except oserror: print 'file %s not exist.' %(fbxfile) homecoming none
this code below i'm running in module called neo_curves.py (this not finish code, , 'my_path' path of current directory neo_curves.py beingness run in)
def create_entry(self, crv): """exports user curve user info directory , adds entry neo_usercurves.py keyword arguments: crv -- (pynode) object export """ # set settings mel.eval('fbxexportfileversion "fbx201400"') mel.eval('fbxexportinputconnections -v 0') select(crv) mc.file('%s\userdat\%s.fbx' %(my_path, str(crv)), force=true, options='', typ='fbx export', pr=true, es=true) open('%s\userdat\\neo_usercurves.py' %(my_path), 'r+') usercrvs: line in usercrvs: if line.strip() == '#crvstart': #linepos = usercrvs.tell() #linepos = int(linepos) #usercrvs.seek(linepos, 0) usercrvs.seek(665, 0) usercrvs.write("\n "+str(crv)+" : '%s\%s' %(my_path, '"+ str(crv)+".fbx')") break
this give me result below:
# filename: neo_usercurves.py # created by: gregory smith # description: script containing library of user created curves # purpose: library store names of user curves, , deletes curves # if specified import os import maya.cmds mc import module_locator my_path = module_locator.module_path() def usercurve_lib(fbxfile=none, remove=none): """all control/curve objects created user keyword arguments: fbxfile -- (string) name of fbx file import remove -- (boolean) remove entry library , delete associated fbx file """ curves_dict = { #crvstart loop_crv : '%s\%s' %(my_path, 'loop_crv.fbx') homecoming curves_dict elif not remove: try: name = mc.file(curves_dict[fbxfile], typ='fbx', i=1, iv=true, pmt=false) homecoming name[0] except runtimeerror: homecoming none else: try: os.remove('%s\%s.fbx' %(my_path, fbxfile)) homecoming '%s.fbx' %(fbxfile) except oserror: print 'file %s not exist.' %(fbxfile) homecoming none
in short: on operating systems can not insert files without rewriting if lengths not same.
have @ long give-and-take here: why can not insert files without additional writes? (i neither mean append, nor over-write)
python-2.7 fwrite
Comments
Post a Comment