Replace string with wildcard in a .txt file with Python

好久不见. 提交于 2019-12-12 03:53:15

问题


I'm still new to scripting and am not sure the best method to accomplish what I am trying to do. This will be the first python script I am trying to write. Please note, I am using Python2.7 for this.

I want to write a batch file for a user with an argument given. This argument will be a path. This path will change everyday and be used to execute job tests. I want to invoke it with Nightly.bat "build path"

Here's what I want to accomplish:

1. Batch file ensures the path exists with health check.
2. Batch file executes the python file with the given variable.
3. Python file finds a string in the testrun script with the build path, and will replace that path with the given variable.
4.Batch file executes the testrun selenium script.

Here is the code Python Code:

test1.txt contents:

blah
This is a first string

nightly.py contents:

import sys
import shutil
import os
import re

tf = open('tmp', 'a+')
string = "This is "

with open('test1.txt') as f:
    for line in f.readlines():
            string = re.sub ('This is .*', 'This is a second string', string)

shutil.copy('test1.txt', 'tmp')
tf.write(string)
f.close()
tf.close()

after executing the nightly.py file, this is the tmp file contents:

blah
This is a first stringThis is a second String

I need to make it so This is a first string is replaced by This is a second string

In the end, the tmp file should have the following contents:

blah
This is a second string

Thanks for continuing to try.

*****************************
* Updated for Kirbyfan64sos *
*****************************

nightly.py contents:

import sys
import shutil
import os

tf = open('tmp', 'a+')
with open('test1.txt') as f:
    for line in f.readlines():
        if line == 'This is*':
            line = 'This is a second string' 
        tf.write(line)
f.close()
tf.close()
shutil.copy('tmp', 'test1.txt')
os.remove('tmp')

回答1:


The code should look like:

import sys
tf = open('tmp', 'a+')
with open('WP8974_AudioDecode.html') as f:
    for line in f.readlines() do:
        if line == '<td>\\frosty\*</td>':
            line = '<td>\\frosty\' + sys.argv[1] + '</td>' 
        tf.write(line)
f.close()
shutil.copy('tmp', 'WP8974_AudioDecode.html')
os.remove('tmp')



回答2:


I finally found the answer...

test1.txt before Nightly.py executed:

blah
blah
This is a first string
blah
blah

BTW tabs make a difference in the code with notepad++

import sys
import os
import re
import shutil

tf = open('tmp', 'a+')

with open('test1.txt') as f:
    for line in f.readlines():
        build = re.sub ('This is.*','This is a second string',line)
        tf.write(build)
tf.close()
f.close()
shutil.copy('tmp', 'test1.txt')
os.remove('tmp')

test1.txt after Nightly.py executed:

blah
blah
This is a second string
blah
blah


来源:https://stackoverflow.com/questions/15864968/replace-string-with-wildcard-in-a-txt-file-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!