Issue
I need to close file, but I can't do that, because I use csv.writer, how can I close file? python
def open_spider(self, spider):
time = dt.now().strftime(TIME_FORMAT)
file_path = self.results_dir / FILE_NAME.format(time)
self.file = csv.writer(open(file_path, 'w'))
self.file.writerow(['Статус', 'Количество'])
Solution
Instead of manually closing the file, it is a good practice to wrap the function under with
.
def open_spider(self, spider):
time = dt.now().strftime(TIME_FORMAT)
file_path = self.results_dir / FILE_NAME.format(time)
with open(file_path, 'w') as file:
self.file = csv.writer(file)
self.file.writerow(['Статус', 'Количество'])
Answered By - Aniket Ray
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.