-1

So I have a function which will essentially run and generate a report and save it with args.current_date filename. The issue I am having is to remove the extension .json from the filename being saved which really is not readable. Since I am passing dict as args, I cannot use the strip() method in order to do this. So far my function is follows :

parser = argparse.ArgumentParser()
parser.add_argument("-c", "--current-date", action="store", required=False)
parser.add_argument("-p", "--previous-date", action="store", required=False)
args = parser.parse_args()

def custom_report(file_names, previous_data , current_data):
    reporting = open('reports/' + args.current_date+ "-report.txt", "w")
    reporting.write("This is the comparison report between the directories" " " + args.current_date +
                    " " "and" " " + args.previous_date + "\n\n")
    for item in file_names:
        reporting.write(item + compare(previous_data.get(item), current_data.get(item)) + "\n")
    reporting.close()

It has saved the file as '2019-01-13.json-report.txt'. I want to be able to get rid of the '.json' aspect and leave it as '2019-01-13-report.txt'

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • 1
    Possible duplicate of [How do I remove a substring from the end of a string in Python?](https://stackoverflow.com/questions/1038824/how-do-i-remove-a-substring-from-the-end-of-a-string-in-python) – Todd Sewell Jan 20 '19 at 16:57

1 Answers1

0

To remove a file name extension, you can use os.path.splitext function:

>>> import os
>>> name = '2019-01-13.json'
>>> os.path.splitext(name)
('2019-01-13', '.json')
>>> os.path.splitext(name)[0]
'2019-01-13'
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103