0

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())
1
  • Could you post the whole traceback? Commented Jun 3, 2017 at 5:40

1 Answer 1

1

In your readConfig function, this line performs a search:

match = re.search(r'(?<=boothNumber=")\d+', line)

and the value of match is used in this line:

sys.stdout.write(re.sub(pattern, match, f2line))

There are two problems here. Firstly, if the search is unsuccessful match will be None, causing to the exception that you report:

 >>> re.sub(r'[a-z]+', None, 'spam')
Traceback (most recent call last):
...
TypeError: decoding to str: need a bytes-like object, NoneType found

Secondly, if match is not None, you are trying to use match itself as replacement string, but match is not a string, it's a match object:

>>> match = re.search(r'[a-z]+', 'spam')
>>> match
<_sre.SRE_Match object; span=(0, 4), match='spam'>  # <=== not a string!
>>> re.sub(r'[a-z]+', match, 'eggs')
Traceback (most recent call last):
  ...
TypeError: decoding to str: need a bytes-like object, _sre.SRE_Match found

You need to call match.group() to get the string that has been matched:

>>> re.sub(r'[a-z]+', match.group(), 'eggs')
'spam'

To summarise:

  • condition the output processing on match not being None
  • use match.group() as the replacement string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.