在阅读文本内容时如何写入文本文件的中间? [英] How do I write to the middle of a text file while reading its contents?
问题描述
很少有我用python代码..如下。
import os
import shutil
data =set filelid [open \ C:/ Sanity_Automation / Work_Project / Output / smokeTestResult \w +] \\\\
puts $ filelid \\\\
close $ filelid \\\
路径= C:\\Sanity_Automation\\RouterTester900SystemTest
如果os.path.exists(路径):
shutil.rmtree('C:\\Sanity_Automation\ \RouterTester900SystemTest\\')
path =C:\\程序文件(x86)
如果os.path.exists(路径):
SRC = C:\\Program文件(x86)\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest
else:
src =C:\\程序文件\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest
DEST = C:\\Sanity_Automation \\RouterTester900SystemTest\\
shutil.copytree(SRC,目标寄存器)
日志=开放('C:\\Sanity_Automation\\RouterTester900SystemTest\\\ \\RouterTester900SystemTest.app.tcl ' 'R +')
log_read = log.readlines()
X = CloseAllOutputFile
相开放(' C:\\Sanity_Automation\\ RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl, 'A +')作为FOUT:
。在log_read行:
如果x在一行:
fout.seek(0,1)
fout.write(\ n)
fout.write(data)
将文件从一个位置复制到另一个位置,在特定文件中搜索关键字并将数据写入文件的代码正在工作...
我的疑问是每当我写。它写入文件的结尾,而不是当前的位置...
例如:说...我从文件复制程序文件保存到文件夹中,并在复制的文件之一中搜索单词CloseAllOutputFile。当找到这个单词时,应该在该位置插入文本而不是文件结尾。
文件的中间是使用 fileinput
模块:
import fileinput
用于fileinput.input(r'C:\Sanity_Automation\ .... tcl,inplace = 1)中的行:
打印行,#保留旧内容
如果x在行中:
打印数据#插入新数据
From fileinput
文档 :
$ b
可选就地过滤:如果关键字参数 inplace = 1 是
传递给fileinput.input()或FileInput构造函数,文件
被移动到一个备份文件,标准输出被定向到输入
文件(如果文件相同名称作为备份文件已经存在,
将被默默取代)。这使得可以编写一个过滤器
来重写其输入文件。如果备份参数是给定的
(通常是backup ='。'),则它指定备份文件的
扩展名,并且备份文件保留在附近;由
默认,扩展名为'.bak',当输出
文件关闭时,它被删除。
在不使用 fileinput
的情况下读取数据到文件名
文件中:
从tempfile导入os
导入NamedTemporaryFile
dirpath = os.path.dirname(filename)
with open filename)作为文件,
NamedTemporaryFile(w,dir = dirpath,delete = False)作为outfile:
用于文件中的行:
print>> outfile, #复制旧内容
如果x在行中:
print>> outfile,data#插入新数据
os.remove(filename)#rename()不在Windows上覆盖
os.rename(outfile.name,filename)
First of all thanks for helping me out with moving files and for helping me in with tcl script.
Small doubt i had with python code.. as below..
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)
This code for copying files from one location to another, searching keyword in particular file and writing data to file is working...
My doubt is whenever i write.. It writes to end of file instead of current location...
Example: Say.. I copied file from program files to sanity folder and searched for word "CloseAllOutputFile" in one of the copied file. when the word found, it should insert text in that position instead of end of file.
A simple way to add data in the middle of a file is to use fileinput
module:
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
From the fileinput
docs:
Optional in-place filtering: if the keyword argument inplace=1 is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup='.'), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed.
To insert data into filename
file while reading it without using 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)
这篇关于在阅读文本内容时如何写入文本文件的中间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!