Reputation: 1
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
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
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
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