Search code examples
pythonpython-3.xfile-iofile-handlingfile-writing

Python Append file should refreshed with new data


I am trying to write my output into a file what my code is doing is that its looking for matched file names and storing it into a file similary for unmatched files but the problem is when i use write it overwrites the file and when i use append on every run it keeps on appending the file matched file names. What i need is that it refresh te file whenever the script is run and loads it with current data only.

    import re
    import sys
    import os
    import glob
    import pandas as pd
    import logging
    
    try:
        for file in glob.glob('*.csv'):
                r = re.search(r'abc_sales_(20[0-9][0-9])-([1-9]|1[0-2]|0[0-9])-([1-9]|1[0-9]|2[0-9]|3[0-1]|0[0-9])-[0-9]{2}_[a-z0-9]{3,5}.csv', file)
                if r:
                    #matched=file
                    print(f'File matched:{file}')
                    fp=open('bad_lines.txt', 'r+')
                    sys.stdout = fp
                
                else:
                    path=f'File not matched:{file}'
                    f=open('filenotmatched.txt','a')
                    f.seek(0)
                    f.truncate()
                    f.write(path+'\n')
                    f.close()
    except Exception as e:
        pass

Solution

  • Suggested changes to your code.

    import re
    import sys
    import os
    import glob
    import pandas as pd
    import logging
    
    # We create new 'bad_lines.txt' and 
    # 'filenotmatched.txt' for each run
    with open('bad_lines.txt', 'w') as f_badlines, open('filenotmatched.txt','w') as f_notmatched:
        try:
            for file in glob.glob('*.csv'):
                    r = re.search(r'abc_sales_(20[0-9][0-9])-([1-9]|1[0-2]|0[0-9])-([1-9]|1[0-9]|2[0-9]|3[0-1]|0[0-9])-[0-9]{2}_[a-z0-9]{3,5}.csv', file)
                    if r:
                        #matched=file
                        #print(f'File matched:{file}')
                        #fp=open('bad_lines.txt', 'r+')
                        # ** Not clear why you redirected 
                        # ** standard out to a file
                        # ** rather than writing to file directly
                        #sys.stdout = fp
                        f_badlines.write(f'File matched:{file}\n')
                    else:
                        path=f'File not matched:{file}'
                        #f=open('filenotmatched.txt','a')
                        #f.seek(0)
                        #f.truncate()
                        #f.write(path+'\n')
                        #f.close()
                        f_notmatched.write(path + '\n')
        except Exception as e:
            pass