I'm trying to implement a python script alowwing me to generate a file "file_modified" based on a the argument given to the script
My script is working fine, but now i would like to implement a "recursive" option similar to "rm -r" : the goal is to give a folder (containing n files) as an argument to my script, and generated n new files based on this folder.
#!/usr/bin/python
''' Python tools to blabla
'''
import argparse
from datetime import datetime
import sys
import pandas as pd
EXTENSION="_friendly_excel"
FILETYPE=".csv"
def get_args():
'''This function parses and return arguments passed in'''
# Assign description to the help doc
parser = argparse.ArgumentParser(
description='python command to transform a former LPBM .csv file to an excel friendly one')
# Add arguments
parser.add_argument(
"file_csv", help="file you wish to convert")
parser.add_argument('-f', "--filename", type=str, required=False, help="specify a name for the output file")
# Assign args to variables
args = parser.parse_args()
file_csv=args.file_csv
filename=args.filename
# Return all variable values
return file_csv, filename
if __name__ == "__main__":
# Run get_args()
# get_args()
# Match return values from get_arguments()
# and assign to their respective variables
file_csv, filename = get_args()
#name of the file :
if filename:
filename=filename+FILETYPE
else:
filename=file_csv[:-4:]+EXTENSION+FILETYPE
# Print the values
print "\nfile you wish to convert : %s \n" % file_csv
#opening the file as a dataframe
try:
df = pd.read_csv(file_csv, sep=';', parse_dates=['date-time'])
except:
print "\nfailed to load the dataframe : are you sure your file is an LPBM generated one ?\n"
exit()
#saving the modified dataframe with correct date format and decimal
df.to_csv(filename, date_format='%d/%m/%Y %H:%M:%S', sep=';', decimal=',', index=False)
print "le fichier %s a bien ete cree" % filename