0

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()
2
  • Maybe you want to enter a full path for the file. Commented Apr 22, 2020 at 15:29
  • where would I put that in at? In the open statement? Commented Apr 22, 2020 at 15:31

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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()

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.