Calculating size of content - python

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?

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.

Related

Python script to remove certain things form a string

i have a file with many lines like this,
>6_KA-RFNB-1505/2021-EPI_ISL_8285588-2021-12-02
i need to convert it to
>6_KA_2021-1202
all of the lines that require this change start in a >
The 6_KA and the 2021-12-02 are different for all lines.
I also need to add an empty line before every line that i change in thsi manner.
UPDATE: You changed the requirements from when I originally answered yourpost, but the below does what you are looking for. The principle remains the same: use regex to identify the parts of the string you are looking to replace. And then as you are going thru each line of the file create a new string based on the values you parsed out from the regex
import re
regex = re.compile('>(?P<first>[0-9a-zA-Z]{1,3}_[0-9a-zA-Z]{1,3}).*(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})\n')
def convert_file(inputFile):
with open(inputFile, 'r') as input, open('Output.txt', 'w') as output:
for line in input:
text = regex.match(line)
if text:
output.write("\n" + text.group("first") + '_' + text.group("year") + "-" + text.group("month") + text.group("day") + "\n")
else:
output.write(line)
convert_file('data.txt')

How does the last part of this Python code do what it does?

Essential what this code does is it takes a .txt file, replaces " " with "|"
then after it replaces it, it then goes back in and deletes any rows that start with a "|"
Im having trouble understanding what the last part of this code does.
I understand everything up until the:
output = [line for line in txt if not line.startswith("|")]
f.seek(0)
f.write("".join(output))
f.truncate()
everything before this code ^^ i understand but im not sure how this code does what its doing.
--------this is the full code----------
# This imports the correct package
import datetime
# this creates "today" as the variable that holds todays date
today = datetime.date.today().strftime("%Y%m%d")
# Read the file
with open(r'\\mlgserver04\mlgshare\DataTransfer&AuditCompliance\ARSI Calls\CallLog_ARSI_' + today + '.txt', 'r') as file:
filedata = file.read()
# Replace the target string
filedata = filedata.replace(' ', '|')
# Write the file out again
with open(r'\\mlgserver04\mlgshare\DataTransfer&AuditCompliance\ARSI Calls\CallLog_ARSI_' + today + '.txt', 'w') as file:
file.write(filedata)
# opens the file
with open(r'\\mlgserver04\mlgshare\DataTransfer&AuditCompliance\ARSI Calls\CallLog_ARSI_' + today + '.txt','r+') as f:
txt = f.readlines()
output = [line for line in txt if not line.startswith("|")]
f.seek(0)
f.write("".join(output))
f.truncate()
f.read() returns a blob of data (string or bytes)
On the other hand, f.readlines() returns a LIST of strings or bytes.
output = [line for line in txt if not line.startswith("|")]
This is the same thing as saying
output = []
for line in txt:
if not line.startswith("|"):
output.append(line)
So, make a NEW list of strings, consisting of only the ones that we want to keep.
"".join(output)
Join takes an iterable of strings, and adds them together with the delimiter (in this case, it was ""). so output[0] + "" + output[1] and so on.
Finally, write that final string back to the file.

Why does it write to 2 lines in a text file when I only ask it to type 1?

Basically I need to write a line to a txt file containing details about a product, these details are from another text file which I split. The final detail which is the quantity variable is an inputted number.
document = open('Task2.txt', 'r')
strquantity = str(quantity)
for line in document:
the_line = line.split(",")
if the_line[0] == GTIN:
with open("receipt.txt", "a") as receipt:
receipt.write(the_line[0] + "," + the_line[1]+ "," +the_line[2] + "," + strquantity)
document.close()
The task 2 file contains:
12345670,spatula,0.99
57954363,car,1000.20
09499997,towel,1.20
The quantity number is 5 and the GTIN number is 12345670. The line it should write to the file is:
12345670,spatula,0.99,5
But instead it writes:
12345670,spatula,0.99,
5
(no line space (five on the next line under))
Why does it do this and how do I make it so it just writes to the 1 line? Thanks.
The reason is because when you read in each line, it will have a newline at the end. So when you call split, the final entry will also contain a newline, so when you write the_list[2] it will split the line at this point. To get around this, call strip() to remove the newline as follows:
with open('Task2.txt', 'r') as document, open("receipt.txt", "a") as receipt:
strquantity = str(quantity)
for line in document:
the_line = line.strip().split(",")
if the_line[0] == GTIN:
receipt.write(','.join(the_line[0], the_line[1], the_line[2], strquantity) + '\n')
you need to trim the newline character from each line just before exploding it.
the_line=line.strip()
the_line=the_line.split(",")

Python: Formatting the way my program writes arrays to txt file

I am trying to get my program to print one item from each array to a text file and then once all the first items were written,write the second item of the array on the second line and so on.
The code I have now only prints the info on one line of text.
def write():
outFile=open("Inventory.txt","w")
for i in range(0,len(clothesItem)):
outFile.write(format(clothesItem[i],ITEM_FORMAT)+format(clothesColor[i],COLOR_FORMAT)+format(clothesAmount[i],AMOUNT_FORMAT))
outFile.close()
Change this line:
outFile.write(format(clothesItem[i],ITEM_FORMAT)+format(clothesColor[i],COLOR_FORMAT)+format(clothesAmount[i],AMOUNT_FORMAT))
To the following:
outFile.write(format(clothesItem[i], ITEM_FORMAT) + format(clothesColor[i],COLOR_FORMAT) + format(clothesAmount[i], AMOUNT_FORMAT) + "\n")
^^^^
Note the + "\n" added onto the end.

How to replace a line in a file with a string in Python, whose length is less than the line to be replaced?

I am trying to replace a line in a file, with a string whose length differs based on the input.
If the length of string is equal to or greater than the line to be replaced,
then the file is overwritten correctly. Whereas, if the string is shorter than the line to be replaced,
then after replacing, part of the line will be appended at the end.
The code snippet used for writing to the file is given below.
replace_value = status
lines = []
filename = open(os.path.join(dst_path, asilSafety_path),'r+')
for line in filename.readlines():
line.strip('\n')
if line.startswith('export SAFETY_SRV_MODE'):
bits = line.split('=')
config, value = bits
src = config + '=' + value.strip('\n')
target = config + '= ' + replace_value
break
filename.seek(0)
for line in filename.readlines():
line = line.replace(src,target)
lines.append(line)
filename.seek(0)
for line in lines:
filename.write(line)
filename.close()
I am passing the file path and the string to be written as arguments to the function i.e., safety_configuration(dst_path, status). The variable 'replace_value' has the string to be replaced with.
Can someone please tell me, wat am i doing wrong? Or is there any other way to implement this?
import fileinput
import sys
count = 0
replace_value = status
for line in fileinput.input(["a.txt"], inplace=True, backup='.bak'):
if line.startswith('export SAFETY_SRV_MODE'):
bits = line.split('=')
config, value = bits
src = config + '=' + value.strip('\n')
target = config + '= ' + replace_value+"\n"
sys.stdout.write(line.replace(src,target))
else:
sys.stdout.write(line)
You can use file input for this instead of moving up and down the file you can change it at the time when you found it
I'd suggest looping over the lines in the file once, and if they don't match what you want to replace, simply append them to lines.
If they do, modify that line before appending it to lines. The simply re-write them out to the file.
replace_value = status
lines = []
with open(os.path.join(dst_path, asilSafety_path)) as f:
for line in f:
if line.startswith('export SAFETY_SRV_MODE'):
config, value = line.split('=')
lines.append(config + '= ' + replace_value + '\n')
else:
lines.append(line)
with open(os.path.join(dst_path, asilSafety_path), 'w') as f:
f.writelines(lines)
Your solution is correct provide you have enough memory to load whole file. You simply forgot to truncate the file object, and that's the reason why you see garbage at the end.
The last part of your script should be :
filename.seek(0)
for line in lines:
filename.write(line)
filename.truncate()
filename.close()
You can also truncate immediately after the seek.

Categories