Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
should I close csv file?
#1
I have a csv file I made some calculation with code function on it. and requesting output is okay write now.

my question is:
should I write the close() at the end of the code? what would be difference when I write that line or I did not write it?

for example:
with open('grades.csv', 'w', newline='') as output_file_name:
    writer = csv.writer(output_file_name)
    writer.writerows(data)
    output_file_name.close()
Reply
#2
this (i.e. using with) is an example of so called context manager. When using context manager you don't need to close the file - it will close it for you.

Just
with open('grades.csv', 'w', newline='') as output_file_name:
    writer = csv.writer(output_file_name)
    writer.writerows(data)
is enough
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
thanks.
also I have another problem in my csv output file.

on writing process the code add some empty line at the end of csv file.

here is a sample of output:

Quote:1-peter,11.285714285714286
2-edmond,9.75
3-
I have numbered the line just to clarify my question, the third line is unnecessary.
Reply
#4
check your data - you will see the last element is empty
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
i have check that, it does not have any empty line. but when I refresh my csv file (after running the code) the new empty line appear in it.

please check here:
https://python-forum.io/Thread-how-can-I...ile?page=2
Reply
#6
sorry, I didn't read carefully that the line numbers are just here, not in the file.
you got empty line, because every line that you write has a newline char at the end

from the docs for csvwriter.writerows:
Quote:Write all elements in rows (an iterable of row objects as described above) to the writer’s file object, formatted according to the current dialect.

and the default dialect lineterminator is '\r\n'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
so it means in any way I will print a new line with this code on my csv file?
Reply
#8
unless you write some weird code to handle line endings the way you want
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
it homework I should use csv in python to reach the preferred results.
hopefully you answer my question sooner that my mentor.

I have a five different function that check the first csv file and rewrite the results on it as an output_file_name.

I have two problem until now.

1-how to remove that empty line in the writing process.
2- how to check the every steps on my computer before sending it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Save and Close Excel File avd88 0 2,840 Feb-20-2023, 07:19 PM
Last Post: avd88
  pyPDF2 PDFMerger close pensding file japo85 2 2,341 Jul-28-2022, 09:49 AM
Last Post: japo85
  How to close file handler or log file on each iteration Mekala 3 5,025 Aug-16-2020, 03:15 PM
Last Post: snippsat
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 1,989 Mar-03-2020, 04:47 AM
Last Post: davidshq
  How can I Open and close .py file from python scripts SayHiii 9 5,625 Dec-17-2019, 06:10 AM
Last Post: Malt
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 7 3,846 Oct-21-2019, 11:28 AM
Last Post: kryptomatrix
  Need help to prove close File slice 4 4,420 May-18-2017, 08:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020