0

my problem here is that I have a huge amount of files. Each xml file contains ID. And I got set of source and target files. Source files has name A and ID = B. Target file has name B and ID=B What I need to do is to match source ID B with Target name B and then replace target ID=B with source name A. Hope its clear

Here is my code

import os
import re

sourcepath = input('Path to source folder:\n')
targetpath = input('Path to target folder:\n')
for root,dir,source in os.walk(sourcepath):
    for each_file in source:
        os.chdir(root)
        correctID = each_file[:16]
        each_xml = open(each_file, 'r', encoding='utf8').read()
        findsourceID = re.findall('id="\w{3}\d{13}"', each_xml)
        StringID = str(findsourceID)
        correctFilename = StringID[6:22]
        IDtoreplace = 'id="' + correctID + '"'
        print(IDtoreplace)
        for main,folder,target in os.walk(targetpath):
            for each_target in target:
                os.chdir(main)
                targetname = each_target[:16]
                if targetname == correctFilename:
                    with open(each_target, 'r+', encoding='utf8') as each_targ:
                        each_targ.read()
                        findtargetID = re.sub('id="\w{3}\d{13}"',str(IDtoreplace), each_targ)
                        each_targ.close()

And here is the error

File "C:/Users/ms/Desktop/Project/test.py", line 23, in <module>
    findtargetID = re.sub('id="\w{3}\d{13}"',str(IDtoreplace), each_targ)
  File "C:\Users\ms\AppData\Local\Programs\Python\Python35\lib\re.py", line 182, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
1
  • Please help I need to complete it today :(:(:( Commented Aug 17, 2016 at 11:12

1 Answer 1

1

You read() from each_targ but you don't store the string anywhere.

Instead you pass the file handle each_targ to .sub and that causes the type mismatch here. You could just say:

findtargetID = re.sub('id="\w{3}\d{13}"',str(IDtoreplace), each_targ.read())
Sign up to request clarification or add additional context in comments.

4 Comments

yes it does not call an error but do not save the file. I mean nothing was changed
what I'm doing wrong that file do not want to save
I did something like that but it adds text rather then edits targetID = (re.sub(r'id="\w{3}\d{13}"', strtarget, each_targ.read())) each_targ.write(targetID) each_targ.close()
@MaciejPL: yes it adds because there's no really in-place file editing. The easiest way should be to open the target file again (close the first one after you have read the contents in memory) with w flag. It will truncate the file into zero size first! Then once you write and close it, it will look as is edited.

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.