jfreck6337
jfreck6337

Reputation: 1

How to get Python Script to send CSV file to specific folder location

I'm currently have an API that calls to a system and produces a CSV file of data from that system. That CSV file is placed to a folder on my computer, however I would like to point it to be placed to a different specific folder on my computer.

I do not have the current folder that it is being placed in specified in the API so I'm not sure why it is being placed there.

I think that I need to change my open statement below. Is there a way for me to write a path into this clause so that it gets placed in a different folder? Or would I need to state that somewhere else?

with open("UserStory.csv", "w", newline="") as outputFile:

    writer = csv.DictWriter(outputFile, ["Number","Estimate", "Project", "CreateDate", "ClosedDate", "CompletedDate", "Sprint", "Sprint Start", "Sprint End","Planned","Team",  "BlockingIssues", "Status"])

    writer.writeheader()

Upvotes: 0

Views: 800

Answers (3)

Nathan Mathews
Nathan Mathews

Reputation: 153

Writing to specific directory using os.path.join

import os

def write_to_dir(file, dir):
    with open(os.path.join(dir, file), "w", newline="") as outputFile:
        os.makedirs(dir, exists_ok=True) #address directory DNE 
        writer = csv.DictWriter(outputFile, ["Number","Estimate", "Project", "CreateDate", "ClosedDate", "CompletedDate", "Sprint", "Sprint Start", "Sprint End","Planned","Team",  "BlockingIssues", "Status"])
        writer.writeheader()

Upvotes: 1

Chets
Chets

Reputation: 100

Specify full path of the file something like this :

file_path = '/home/user/doc/file_name.csv'

with open(file_path, "w", newline="") as outputFile:

if dir does not exist, create one using: os.mkdir(dir_path)

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

When you specify just a file name with "UserStory.csv" it will be stored in the so-called "current working directory". Most likely this is the same folder that contains your python script.

To save the directory in another folder, you can specify an absolute path:

with open(r"C:\Users\my_user\Documents\my_csv_files\UserStory.csv", "w", newline="") as outputFile:

I suggest you take some time to learn about directory paths. You should learn the difference between absolute paths and relative paths.

Upvotes: 1

Related Questions