python – 如何在读取文本内容时写入文本文件的中间?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何在读取文本内容时写入文本文件的中间?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2708字,纯文字阅读大概需要4分钟。
内容图文
![python – 如何在读取文本内容时写入文本文件的中间?](/upload/InfoBanner/zyjiaocheng/732/4bf6bfe9e0e6425b8d3bbb54d0f64dce.jpg)
首先感谢帮助我移动文件和帮助我使用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”.当找到单词时,它应该在该位置插入文本而不是文件末尾.
解决方法:
在文件中间添加数据的简单方法是使用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
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.
在不使用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)
内容总结
以上是互联网集市为您收集整理的python – 如何在读取文本内容时写入文本文件的中间?全部内容,希望文章能够帮你解决python – 如何在读取文本内容时写入文本文件的中间?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。