3

首先感谢您帮助我移动文件并帮助我使用 tcl 脚本。

我对python代码有一点怀疑..如下..

import os
import shutil

data =" set filelid [open \"C:/Sanity_Automation/Work_Project/Output/smokeTestResult\" w+] \n\
        puts $filelid \n\
        close $filelid \n"  

path = "C:\\Sanity_Automation\\RouterTester900SystemTest"
if os.path.exists(path):
    shutil.rmtree('C:\\Sanity_Automation\\RouterTester900SystemTest\\')

path = "C:\\Program Files (x86)"
if os.path.exists(path):
    src= "C:\\Program Files (x86)\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
else:
    src= "C:\\Program Files\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
dest = "C:\\Sanity_Automation\\RouterTester900SystemTest\\"

shutil.copytree(src, dest)
log = open('C:\\Sanity_Automation\\RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl','r+')
log_read=log.readlines()
x="CloseAllOutputFile"
with open('C:\\Sanity_Automation\\RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl', 'a+') as fout:
        for line in log_read:
          if x in line:
            fout.seek(0,1)
            fout.write("\n")
            fout.write(data)

此代码用于将文件从一个位置复制到另一个位置,在特定文件中搜索关键字并将数据写入文件正在工作......

我的疑问是每当我写..它写入文件末尾而不是当前位置...

示例:说..我将文件从程序文件复制到 sanity 文件夹并在复制的文件之一中搜索单词“CloseAllOutputFile”。当找到单词时,它应该在该位置插入文本而不是文件末尾。

4

3 回答 3

6

在文件中间添加数据的一种简单方法是使用fileinput模块:

import fileinput

for line in fileinput.input(r'C:\Sanity_Automation\....tcl', inplace=1):
    print line, # preserve old content
    if x in line:
       print data # insert new data

文档_fileinput

可选的就地过滤:如果关键字参数inplace =1被传递给 fileinput.input() 或 FileInput 构造函数,则文件被移动到备份文件并且标准输出被定向到输入文件(如果与备份文件同名的文件已存在,它将被静默替换)。这使得编写一个过滤器来重写其输入文件成为可能。如果给定了备份参数(通常为backup='.'),它指定备份文件的扩展名,并且备份文件保持不变;默认情况下,扩展名为“.bak”,并在输出文件关闭时被删除。

要在读取文件时将数据插入filename文件而不使用fileinput

import os
from tempfile import NamedTemporaryFile

dirpath = os.path.dirname(filename)
with open(filename) as file, \
     NamedTemporaryFile("w", dir=dirpath, delete=False) as outfile:
    for line in file:
        print >>outfile, line, # copy old content
        if x in line:
           print >>outfile, data # insert new data

os.remove(filename) # rename() doesn't overwrite on Windows
os.rename(outfile.name, filename)
于 2013-05-15T10:46:54.630 回答
1

不能做的就是读入文件,插入你想要的文本,然后写回

with open("some_file","r") as f:
     data = f.read()
some_index_you_want_to_insert_at = 122
some_text_to_insert = "anything goes here"

new_data = data[:some_index_you_want_to_insert_at] + some_text_to_insert + data[some_index_you_want_to_insert_at:]

with open("some_file","w") as f:
     f.write(new_data)

print "all done!"
于 2013-05-15T05:25:35.197 回答
0

实际上,您的方法有效,但您的 fout 以附加模式打开。所以这就是为什么你只能写在最后。这是一个工作示例。

# creating a file for reference
ff = open("infiletest","w")
pos_file = {}
for i in range(3):
    pos_file[i] = ff.tell()
    ff.write("%s   21/1/1983\n" % i)
ff.close()

# modify the second line
ff = open("infiletest","r+")
ff.seek(pos_file[2])
ff.write("%s   00/0/0000\n" % 2)
ff.close()

请注意,您覆盖了文件的内容。

于 2015-02-22T21:18:15.813 回答