Reputation: 2757
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
?
Upvotes: 1
Views: 75
Reputation: 165232
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.
Upvotes: 1