简体   繁体   中英

execfile issue with the filename argument in python

I have two python scripts. One match script, the other is regex script. I want to start with my matchParser.py script and then go to regexParser.py. I want that my regexParser knows the filename of the matchParser and continue to use. I hope I could explain it clearly. I tried a lot, but unfortunately without success.

My OutputError: TypeError: coercing to Unicode: need string or buffer, file found

matchParser.py

import glob

intfiles = glob.glob("C:\Users\x\Desktop\x\*.csv")

header_saved = False
with open('outputdre121.csv','wb') as fout:
    for filename in intfiles:
        with open(filename) as fin:
            header = next(fin)
            if not header_saved:
                fout.write(header)
                header_saved = True
            for line in fin:
                fout.write(line)
print "start next script: regexParser.py"
execfile("regexParser.py")

regexParser.py

import re
import matchParser

lines = [line.strip() for line in open(matchParser.fout)] ## here the filename from MatchParser.py

with open('outputregexdre13.csv', "w") as output_csv:
    for result in lines:
        match = re.search(r'((\s\d+?[1-9])!?[ ])', result)
        if match: output_csv.write(match.group(0) + '\n')

thanks!

I have found the solution: Very simple actually ..

I add to matchParser.fout the .name method

lines = [line.strip() for line in open(matchParser.fout.name)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM