bingo.config有一个字符串,里面有一个随机整数。 在文件内部,它看起来像
boothNumber="2"
我的template.config文件具有相同的字符串,其中包含不同的整数或数字。 我试图用bingo.config
中的当前整数替换它我的问题是template.config中的数字没有被bingo.config中的数字替换
它正在使用文字字符串“r”(?< = boothNumber =“)\ d +'”
替换template.config中的数字所以template.config最终看起来像
boothNumber="r'(?<=boothNumber=")\d+'"
而不是
boothNumber="2"
看起来我的问题源于我如何在“readconfig”函数中保存变量“pattern”。
关于如何将bingo.config中的整数保存到可以使用的适当变量的任何想法?
import os
import shutil
import fileinput
import re # used to replace string
import sys # prevents extra lines being inputed in config
# example: sys.stdout.write
def convertconfig(pattern):
source = "template.config"
with fileinput.FileInput(source, inplace=True, backup='.bak') as file:
for line in file:
match = r'(?<=boothNumber=")\d+'
sys.stdout.write(re.sub(match, pattern, line))
def readconfig():
source = "bingo.config"
pattern = r'(?<=boothNumber=")\d+' # !!!!!!!!!! This probably needs fixed
with fileinput.FileInput(source, inplace=True, backup='.bak') as file:
for line in file:
if re.search(pattern, line):
fileinput.close()
convertconfig(pattern)
def copyfrom(servername):
source = r'//' + servername + '/c$/remotedirectory'
dest = r"C:/myprogram"
file = "bingo.config"
try:
shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))
except:
print ("Error")
readconfig()
# begin here
os.system('cls' if os.name == 'nt' else 'clear')
array = []
with open("serverlist.txt", "r") as f:
for servername in f:
copyfrom(servername.strip())