0

How can I append a csv file with new information without overwritting the existing results? I tried but it overwrites:

def save_jobs_to_excel(jobs_list, filename):
path=r'\Users\Alberto\OneDrive\Documents\Big Data\pirple\jobs'
jobs = pd.DataFrame(jobs_list)

if filename not in path:

    jobs.to_csv(os.path.join(path,f'{filename}.csv'))
elif filename in path:
    with open(filename,'a') as f:
        f.write(jobs.to_csv(os.path.join(path,f'{filename}.csv')))
else: 
    pass
AML
  • 19
  • 8

1 Answers1

0

I think you are double opening your file and then having jobs.to_csv() overwrite. Maybe something like:

def save_jobs_to_excel(jobs_list, filename):
    path = r'\Users\Alberto\OneDrive\Documents\Big Data\pirple\jobs'
    jobs = pd.DataFrame(jobs_list)

    filepath = os.path.join(path,f'{filename}.csv')
    if filepath.exists():
        jobs.to_csv(filepath)
    elif filename in path:
        jobs.to_csv(open(filepath,'a'))
    else: 
        pass
Bobby Ocean
  • 3,120
  • 1
  • 8
  • 15