I have an original config file that has a string:
boothNumber="5"
In my program, I grab a similar config from another computer. This similar config has a string:
boothNumber="1"
I want to read the new number which is 1 and replace the original config with the number 1(replaces 5).
I am getting an error in my program that says:
TypeError: coercing to str: need a bytes-like object, NoneType found
Any ideas?
import os
import shutil
import fileinput
import pypyodbc
import re # used to replace string
import sys # prevents extra lines being inputed in config
def readconfig(servername):
destsource = 'remote.config' # file that I grabbed from remote computer
template = 'original.config' # original config
for line in open(destsource): # open new config
match = re.search(r'(?<=boothNumber=")\d+', line) #find a number after a certain string and name it 'match'
with fileinput.FileInput(template, inplace=True, backup='.bak') as file: # open original config
for f2line in file:
pattern = r'(?<=boothNumber=")\d+' # find number after certain string and name it 'pattern'
if re.search(pattern, f2line): # if line has 'pattern'
sys.stdout.write(re.sub(pattern, match, f2line)) # replace 'pattern' number with number from 'match'
fileinput.close()
def copyfrom(servername):
# copy config from server
source = r'//' + servername + '/c$/configdirectory'
dest = r"C:/myprogramdir"
file = "remote.config"
try:
shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))
# you can't just use shutil when copying from a remote computer. you have to also use os.path.join
except:
copyerror()
readconfig(servername)
os.system('cls' if os.name == 'nt' else 'clear')
array = []
with open("serverlist.txt", "r") as f: # list of computer names
for servername in f:
copyfrom(servername.strip())