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 8827943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:29:38+00:00 2026-06-14T07:29:38+00:00

Below is the code snippet. I have a file. f = open(self.reportSavePath,’w’) self.test =

  • 0

Below is the code snippet. I have a file.

f = open(self.reportSavePath,'w')
self.test = '';
for file in file_sorted:
    f.write(str(os.path.getmtime(file)) + "|" + file + "\r\n")
    self.test = self.test + str(os.path.getmtime(file)) + "|" + file + "\r\n"
f.close()

print("Size:",os.path.getsize(self.reportSavePath)) #Without opening file
print("Calculated size:",len(self.test.encode()))     #After reading the contents

My question is, why are the last two lines giving different output? Should they not be same?

If there is a reason, how can I edit line with comment #Without opening file to match the output with line commented #After reading the contents?

  • 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-06-14T07:29:39+00:00Added an answer on June 14, 2026 at 7:29 am

    You’re comparing apples and oranges.

    os.path.getsizeof returns the file size. len(some_string) returns the length of the string in characters regardless of encoding, which can affect the naive byte count.

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

Sidebar

Related Questions

I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
I have managed to use the below snippet of code, to open a pdf
I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
I have this snippet of code below. I want to pass the value of
I have below snippet of code in which TestClass is extending jPanel which is
Below is the snippet of my code. I have a JTable. I have extended
I have the following code snippet, which loads data from a CSV file into
I have a code snippet below from an exercise I'm working on. It reads
In the code snippet below, I only have 1 element in the XML that
Below is a code snippet from the file I am working with. I will

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.