2014-11-02 71 views
1

我有一個模塊,我想寫入。我有幾個問題。其中一個在文件中查找字符串。目前我打開文件,然後在(文件名)中使用for行,然後執行if來確定它是否找到一個字符串,並且所有這些都有效。然而,之前(它現在被註釋掉了)我試圖確定它在使用tell()時的位置。然而,這給了我一個不正確的立場,給我1118我相信,而不是660的東西。所以我手動確定位置以使用seek。在文本文件中的字符之間寫入?

但是,第二個問題是,如果我在文件的位置寫入此文件,它只是覆蓋其上的所有數據。我想要插入數據而不是覆蓋它。

除非我插入一個字符串等於字符長度,我希望寫入發生,它將只是覆蓋大部分if語句和類似下面的東西。

有沒有辦法天真地做到這一點?

這裏是我想寫到

# Filename: neo_usercurves.py 
# Created By: Gregory Smith 
# Description: A script containing a library of user created curves 
# Purpose: A library to store names of all the user curves, and deletes curves 
# if specified to do so 

import os 
import maya.cmds as mc 
import module_locator 

my_path = module_locator.module_path() 

def usercurve_lib(fbxfile=None, remove=None): 
    """All control/curve objects created by user 

    Keyword Arguments: 
    fbxfile -- (string) name of fbx file to import 
    remove -- (boolean) will remove an entry from the library and delete the 
     associated fbx file 
    """ 
    curves_dict = { 
    #crvstart 

    #crvend 
    } 
    if remove is None: 
     return curves_dict 
    elif not remove: 
     try: 
      name = mc.file(curves_dict[fbxfile], typ='FBX', i=1, 
       iv=True, pmt=False) 
      return name[0] 
     except RuntimeError: 
      return None 
    else: 
     try: 
      os.remove('%s\%s.fbx' %(my_path, fbxfile)) 
      return '%s.fbx' %(fbxfile) 
     except OSError: 
      print 'File %s does not exist.' %(fbxfile) 
      return None 

此文件是低於我在一個模塊調用neo_curves.py運行的代碼(這是不完整的代碼,以及「my_path的」是僅僅是當前目錄neo_curves.py的路徑正在被運行)

def create_entry(self, crv):  

    """Exports user curve to user data directory and adds entry into 
     neo_usercurves.py 

     Keyword Arguments: 
     crv -- (PyNode) the object to 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) 
     with open('%s\userdat\\neo_usercurves.py' %(my_path), 'r+') as usercrvs: 
      for 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 

這會給我下面這樣的結果:

# Filename: neo_usercurves.py 
# Created By: Gregory Smith 
# Description: A script containing a library of user created curves 
# Purpose: A library to store names of all the user curves, and deletes curves 
# if specified to do so 

import os 
import maya.cmds as mc 
import module_locator 

my_path = module_locator.module_path() 

def usercurve_lib(fbxfile=None, remove=None): 
    """All control/curve objects created by user 

    Keyword Arguments: 
    fbxfile -- (string) name of fbx file to import 
    remove -- (boolean) will remove an entry from the library and delete the 
     associated fbx file 
    """ 
    curves_dict = { 
    #crvstart 
    loop_crv : '%s\%s' %(my_path, 'loop_crv.fbx')  return curves_dict 
    elif not remove: 
     try: 
      name = mc.file(curves_dict[fbxfile], typ='FBX', i=1, 
       iv=True, pmt=False) 
      return name[0] 
     except RuntimeError: 
      return None 
    else: 
     try: 
      os.remove('%s\%s.fbx' %(my_path, fbxfile)) 
      return '%s.fbx' %(fbxfile) 
     except OSError: 
      print 'File %s does not exist.' %(fbxfile) 
      return None 
+0

可能重複:http://programmers.stackexchange.com/questions/237416/why-can-we-not-insert-into-files-without-在-附加寫入-I-既不均值 – User 2014-11-02 03:26:00

回答

相關問題