Robert Marciniak
Robert Marciniak

Reputation: 303

creating a new csv file with open() in specific directory

My script creates a new csv file based on the date. It default saves the file the the local directory. Any idea how i can save it to a specific folder? Thx

    with open('record ' + str(current) + '.csv', 'a', newline='') as f:

Upvotes: 0

Views: 426

Answers (2)

dobleD
dobleD

Reputation: 1

the file parameter in the open function is the path to such file. By indicating only the filename it's assumed as a file on the actual directory, so for writing it anywhere on your drive (as long as you have permissions) you should use full path

with open('/tmp/record ' + str(current) + '.csv', 'a', newline='') as f:

Upvotes: 0

rdas
rdas

Reputation: 21275

The simplest way would be to add to the open functions path argument like so:

with open(folder_path + '/' + 'record ' + str(current) + '.csv', 'a', newline='') as f:

Assuming that folder_path holds a path to the specified folder like '/home/users/you/somefolder'

But I would suggest looking into using PathLib for path creation & manipulation

Upvotes: 1

Related Questions