0

I have a Python script as shown below which reads four files and give the difference between those files for each clientId and it works fine:

#!/usr/bin/python
import os, sys, re, json

with open(sys.argv[1]) as old_primary, open(sys.argv[2]) as new_primary, \
    open(sys.argv[3]) as old_second, open(sys.argv[4]) as new_second:

    prepare_json = lambda f: json.loads(re.sub(r'([0-9]+)=', '"\\1":', f.read())) 
    old_pr_data = prepare_json(old_primary)
    new_pr_data = prepare_json(new_primary)
    old_snd_data = prepare_json(old_second)
    new_snd_data = prepare_json(new_second)

    k = sys.argv[5]
    print('ClientId ' + k)
    print('pri=({})'.format(' '.join(map(str, set(old_pr_data[k]) ^ set(new_pr_data[k])))))
    print('snd=({})\n'.format(' '.join(map(str, set(old_snd_data[k]) ^ set(new_snd_data[k])))))

Here is how I am running it:

python test.py old_primary_mapping.txt new_primary_mapping.txt old_secondary_mapping.txt new_secondary_mapping.txt 1

Problem Statement

This is the output I get for clientId 1 after running above script:

ClientId 1
pri=(192 196 176)
snd=(1482 1485 1491 1494)

Now I want to iterate pri and snd array and make a file string like as shown below and delete those files if it exists. I am having * regex in the file.

For pri, delete these files:

/primary/proc_192_for_*.log
/primary/proc_196_for_*.log
/primary/proc_176_for_*.log

For snd, delete these files:

/secondary/proc_1482_for_*.log
/secondary/proc_1485_for_*.log
/secondary/proc_1491_for_*.log
/secondary/proc_1494_for_*.log

I just need to make sure that I am deleting these files only what is there in pri and snd array. Is this possible to do in python?

1 Answer 1

2

You can delete those files by using glob like (untested):

for dir_name, numbers in (('primary', pri), ('secondary', snd)):
    for number in numbers:
        for filename in glob.glob('/{}/proc_{}_for_*.log'.format(dir_name, number)):
             os.unlink(filename)
Sign up to request clarification or add additional context in comments.

5 Comments

When I run this, I get an error like this NameError: name 'pri' is not defined
yeah I just realized it. So how can I do this in my case? I dont have proper array since I am just printing it out.
try map(int ... instead of (map(str ... or just don't call the join to get a list.
after making that change, I get this error print('pri=({})'.format(' '.join(map(int, set(old_pr_data[k]) ^ set(new_pr_data[k]))))) TypeError: sequence item 0: expected string, int found
don't do the join.

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.