-1

I have a python script that removes duplicate of a file which is given by -i parameter to the script. So like -i input.txt.

I want the -o parameter, which is the output filename, be like this: input-file-name-without-extension_dupsremoved.txt. Like -o input_dupsremoved.txt.

removeDups.py

import optparse


def removeDups(inputfile, outputfile):
        file1 = open(inputfile, "r")
        lines = file1.readlines()
        file1.close()
        out = open(outputfile, "w")
        out.writelines(uniquelines(lines))
        out.close()


def uniquelines(lineslist):
        unique = {}
        result = []
        for item in lineslist:
        if item.strip() in unique: continue
        unique[item.strip()] = 1
        result.append(item)
    return result

def main():
        parser = optparse.OptionParser('usage %prog ' +\
                        '-i <inputfile> -o <outputfile>')
        parser.add_option('-i', dest='inputfile', type='string',
                        help='specify your input file')
        parser.add_option('-o', dest='outputfile', type='string',
                        help='specify your output file')
        (options, args) = parser.parse_args()
       inputfile = options.inputfile
        outputfile = options.outputfile
        if (inputfile == None) or (outputfile == None):
                print (parser.usage)
                exit(1)
        else:
                removeDups(inputfile, outputfile)

if __name__ == '__main__':
        main()

Im currently using this batch file to do it but the batch file is not correct.

@echo off
set /p input= Input file: 
set output= output.txt
set dupsremoved= %input%_dupsremoved.txt
script1.py -i %input% -o %output%
script2.py -i %output% -o %dupsremoved%
del %output%

which can be fixed by this answer from Gerhard Barnard:

@echo off
setlocal enabledelayedexpansion
set /p "input=Input file: " 
set "output=output.txt"
set "ext=%input:*.=%
set "input=!input:.%ext%=!
set "dupsremoved=!input!_dupsremoved.txt"

script1.py -i %input% -o %output%
script2.py -i %output% -o %dupsremoved%
del %output%

2 Answers2

0

If you want to continue using your batch file, then here you go. This method uses delayedexpansion

@echo off
setlocal enabledelayedexpansion
set /p "input=Input file: " 
set "output=output.txt"
set "ext=%input:*.=%
set "input=!input:.%ext%=!
set "dupsremoved=!input!_dupsremoved.txt"

script1.py -i %input% -o %output%
script2.py -i %output% -o %dupsremoved%
del %output%

Also note the issues in your script, for instance the spaces after the = in the set command. if you have whitespace in the set command, they will be in your value as well.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

If you want to change input.txt into input_dupsremoved.txt you can use the following code:

txt1 = "input.txt".split(".")[0]
txt2 = txt1 + ("_dupsremoved.txt")

For these kind of operations I recommend looking at the official documentation for strings: https://docs.python.org/3/library/string.html

Heiko Becker
  • 556
  • 3
  • 16