简体   繁体   中英

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:

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM