Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3241800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:14:27+00:00 2026-05-17T18:14:27+00:00

Python newbie getting a bit frustrated with the csv module. At this rate, it

  • 0

Python newbie getting a bit frustrated with the csv module. At this rate, it would have been easier if I wrote the file parser myself, but I want to do things the Pythonic way ….

I have written a little python script that should save my data into a CSV file.

Here is a snippet of my code:

import csv

wrtr = csv.writer(open('myfile.csv','wb'),delimiter=',', quotechar='"')

for row in rows:
    wrtr.writerow([row.field1,row.field2,row.field3])

The file myfile.csv is created successfully, yet it is empty – but has a lock on it, as its still being used by the Python process. It seems that the data has been written to the file in memory, but it has not yet been flushed to disk.

Since the Python process is holding a lock on the file, then I assume that I am responsible for releasing the lock. Here are my questions:

  1. How do I get python to flush to disk
  2. How do I close the file that was opened in the csv.writer() method?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:14:27+00:00Added an answer on May 17, 2026 at 6:14 pm

    Use

    with open('myfile.csv','wb') as myfile:
        wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
        for row in rows:
            wrtr.writerow([row.field1,row.field2,row.field3])
            myfile.flush() # whenever you want
    

    or

    myfile = open('myfile.csv','wb')
    wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
    for row in rows:
        wrtr.writerow([row.field1,row.field2,row.field3])
        myfile.flush() # whenever you want, and/or
    myfile.close() # when you're done.
    

    The nice thing about the first approach is that your file will also be automatically properly closed in case of an Exception.

    If you want your file object to be anonymous, then it will only be closed when the program exits. When or whether it is flushed depends on the OS – so it might be never until exit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie to python and the app engine. I have this code that
I am a Python newbie. I have this small problem. I want to print
I am a newbie to Python and have come across the following example in
i have installed lxml2.2.2 on windows platform(i m using python version 2.6.5).i tried this
Newbie question (I'm just getting started with Python and Pydev): I've created a project
I'm a Python newbie, so bear with me :) I created a file called
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
I've been stuck on this likely very simple problem, but haven't gotten anywhere with
What would be a good set of programming exercises that would help Python newbies
I'm a newbie to Google App Engine but have played around with Django in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.