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

  • Home
  • SEARCH
  • 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 937985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:34:00+00:00 2026-05-15T21:34:00+00:00

myfile = open(‘out.txt’, ‘a’) myfile.write(caption) I write something like this whenever my script is

  • 0
myfile = open('out.txt', 'a')  
myfile.write(caption)

I write something like this whenever my script is run. But after the first run, it keeps on adding same data to the file. How can I check if it’s empty or not and tell it to write only if it’s empty?

  • 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-15T21:34:00+00:00Added an answer on May 15, 2026 at 9:34 pm

    If you want clear out any existing data in the file first and write your output whether the file was empty or not, that’s easy: just use

    open('out.txt', 'w')
    

    'w' means “write”, 'a' means “append”.

    If you want to check whether the file has data in it and not write anything at all if there is data, then Hypnos’ answer would be appropriate. Check the file size and skip the file-writing code if the file is not empty.

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

Sidebar

Related Questions

I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
This is my partial code: if(action==auth) { myfile.open(account.txt); while(!myfile.eof()) { getline(myfile,sline); vector<string> y =
Let's say I have the following code, file1 = open(myfile,w) #Write to file1... #Open
I compile my file with javac myfile.java But when I run myfile.class file with
What is the difference between these two code snippets? open (MYFILE, '>>data.txt'); open (MYFILE,
First Question I tried to do the following myfile.open(file.txt); while(!myfile.eof()) { getline(myfile,sline); delimiter =
So I try such code: std::ofstream myfile; myfile.open (example.txt, std::ios_base::app ); myfile << Request
myfile = open(wrsu+str(i)+'_'+str(j)+'_'+str(TimesToExec)+.txt,'w') sys.stdout = myfile p1 = subprocess.Popen([pathname,r, s,u,str(i),str(j),str(runTime)],stdout=subprocess.PIPE) output = p1.communicate()[0] print
#!usr/bin/perl @array = (); open(myfile,sometext.txt); while(<myfile>) { chomp; push(@array,[split( )]); } close(myfile); print @array[0];
I am open and close an excel book fro mC# like this Microsoft.Office.Interop.Excel.Application oXL

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.