3
\$\begingroup\$

This code needs to get two inputs:

  • one is the full address of a directory
  • and another the file extension that it will be changed.

These inputs cab be added in the default section of parser.add_argument as in the example. It will overwrite the files with the new copyright:

import csv
import argparse
import os


class change_logo(object):

    def __init__(self, key):
        self.keyword = key
        self.case = 1
        self.counter1 = 0
        self.counter2 = 0
        self.counter3 = 0

    def read_line(self, key):
        with open(key, 'rb') as f:
            reader = csv.reader(f, delimiter=',')
            lista = []
            s_index = -2
            for row in reader:
                lista.append(row)

            for index in range(0, len(lista)):

                if any("# Copyright:    (c) 2011 Comp1 Ltd.  All Rights Reserved" in s for s in lista[index]):
                    s_index = index
                    self.counter1 =  self.counter1+ 1

                if any("# Copyright:    (c) 2018 Comp2 Ltd.  All Rights Reserved" in s for
                    s in lista[index]):

                    print("COPYRIGHT ALREADY EXISTS")
                    self.case = 2
                    self.counter2 = self.counter2 + 1
                    return s_index

            if s_index == -2:

                self.counter3 = self.counter3 + 1
                print("NO COPYRIGHT IN THIS FILE")

            else:

                print(s_index + 1)

            return s_index

    def replace_copyright(self, key):

        s_index = self.read_line(key)

        if self.case == 2:
            self.case = 1
            return 0

        if s_index != -2:

            s = open(key).read()
            s = s.replace('# Copyright:    (c) 2011 Comp1 Ltd.  All Rights Reserved',
                      '# Copyright:    (c) 2018 Comp2 Ltd.  All Rights Reserved')
            f = open(key, 'w')
            f.write(s)
            f.close()
            print("COPYRIGHT ADDED IN LINE %d OF THE FILE" % (s_index + 1))

        else:
            s = open(key).read()
            f = open(key, 'w')
            f.write('# Copyright:    (c) 2018 Comp2 Ltd.  All Rights Reserved' + '\n' + s)
            f.close()
            print("COPYRIGHT ADDED IN THE BEGINNING OF THE FILE")


# This class returns a list of addresses

class file_traverser(object):

    # The user sends the path address and file extension that wants changed

    def __init__(self, key):
        self.path = key[0]
        self.file_extension = key[1]

    def file_walker(self):

        directory = []

        # Iterate through all file paths and append the list

        for root, dirs, files in os.walk(self.path):

            for file_ in files:
                if self.file_extension in os.path.join(root, file_):

                    directory.append(os.path.join(root, file_))

        return directory

# This method parses user's arguments from cmd prompt

def parser_arg():
    parser = argparse.ArgumentParser('--help')
    parser.add_argument(
        "--path", nargs="+",
        help="Give a valid directory path and a fileextension to be scanned and changed",
        default=['C:\Users',  ".py"]    # put a directory path and also you can change the extension to .csv or anything else
    )
    args = parser.parse_args()
    path = args.path
    return path


if __name__ == '__main__':

    counter = 0
    counter1, counter2, counter3 = 0, 0, 0
    test1 = file_traverser(parser_arg())
    files = test1.file_walker()

    for index in range(0, len(files)):
        try:


            print(files[index])
            test = change_logo(files[index])
            test.replace_copyright(files[index])
            counter = counter + 1
            counter1 = counter1 + test.counter1
            counter2 = counter2 + test.counter2
            counter3 = counter3 + test.counter3

        except Exception:
            index = index + 1

    print ("number of files read : %d" % counter)
    print ("number of files replaced with the new COPYRIGHT : %d" % counter1)
    print ("number of files that COPYRIGHT ALREADY EXISTS : %d" % counter2)
    print ("number of files  that NO COPYRIGHT EXISTS : %d" % counter3)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.