I have a python script that is as follows:
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="This program parses CSV file for Agris Input from the files that are recieved from BOA",
epilog="*** Important not, leave the .csv off of the second argument so it does not end up in the file name.**")
parser.add_argument('Input', help="Enter the name of the CSV file no extension.")
parser.add_argument('Output', help="This is the output file that will be split into multiple files in the formation <outfile>_dataset_{number}.csv \nLeave the .csv extension off.")
args = parser.parse_args()
import pandas as pd
import os
import sys
fileName = args.Input + ".csv"
outfile = args.Output
# Read data from the input file
data = pd.read_csv(fileName, sep='|', header=0)
if os.path.exists(outfile+".csv"):
append_write = 'a' #appends if exists
with open(output + group in data.groupyb('Number1')+'.csv', append_write) as f:
group.to_csv(f, header=false, index=False, sep='|', float_format='%.2f')
else:
append_write = 'w' #make a new file if it does not exist
for Number1, group in data.groupby('Number1'):
group.to_csv(f'{outfile}_{Number1}.csv', header=True, index=False, sep='|', float_format='%.2f')
I'm wanting if the file exist that is created from this append the data to that file, if it's not there then create a new file.
so example would be
python inOut.py book1 abc #book1 is a csv | delimited file breaking on column called Number1
so output would create abc_100.csv
then when you run the inOut.py book2 abc
, it will look to see if that abc_100.csv
file exists and if it does append the data.
The code above will overwrite the data and not append to it.