0

I currently have a program that collects data from .txt files in a folder and then saves that data to a csv file. Due to how I am planning on distributing this program, I need the Python file to live in the folder where these .txt files are located. However, I need the .csv files to be thrown to an absolute file path rather than being created in the same folder as the Python script and .txt documents. Here is what I have currently coded,

def write_to_csv(journal_list):
    #writes a list of journal dictionaries to a csv file.
    import csv

    username = "Christian"  
    csv_name = username + ".csv" 

    myFile = open(csv_name, 'w')  
    with myFile:  
        myFields = ["filename", "username", "project_name", "file_path",
            "date", "start_time", "end_time", "length_of_revit_session",
            "os_version", "os_build", "revit_build", "revit_branch",
            "cpu_name", "cpu_clockspeed", "gpu_name", "ram_max", "ram_avg", "ram_peak",
            "sync_count", "sync_time_total", "sync_time_peak", "sync_time_avg",
            "commands_total", "commands_hotkey_percentage", "commands_unique",
            "commands_dynamo", "commands_escape_key", "commands_most_used"]
        writer = csv.DictWriter(myFile, fieldnames=myFields)    
        writer.writeheader()
        for item in journal_list:
            try:
                writer.writerow(item)
            except:
                print("error writing data to:", item)

I appreciate the help.

4
  • Where do you define myFile? Commented May 6, 2018 at 5:27
  • csv_name = '/some/absolute/path/%s.csv' % username Commented May 6, 2018 at 5:31
  • @JohnGordon Thank you, you are a genius! Could you add that as an answer so that I can mark this as solved? Thanks a ton Commented May 6, 2018 at 5:51
  • @chrisz Sorry I had modified my original definition to be simpler just to ask this question. I will re add that variable, I forgot to change it. Commented May 6, 2018 at 5:55

2 Answers 2

3

USing os.path.join() you can select your desire path for your file to be written. Here is an example:

import os
desier_path = '/home/foo/'
file_path = os.path.join(dest_dir, csv_name)
with open(file_path, 'w'):
...
Sign up to request clarification or add additional context in comments.

Comments

0

Consider asking for the path from the script, and setting a default if not passed in. This would make your script a lot more flexible than having the path coded in it.

You can use the click package which simplifies this a bit.

import os
import click

def write_to_csv(path, journal_list):
    # .. your normal code
    file_path = os.path.join(path, '{}.csv'.format(username))
    # .. rest of your code here

@click.command()
@click.option('--output_dir', default='/home/foo/bar/', help='Default path to save files')
def main(output_dir):
   write_to_csv(output_dir)

if __name__ == '__main__':
   main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.