I have code like below that checks each file in the list and returns word quantity as a result (which is how many records in the file were found). I would like this result to be overwritten to the previously opened file and save the result from the code there.
Unfortunately, I have a problem with that.
I have files where I have data as below:
abc
cds
cds
abf
Below code as result give:
abc : 1
cds : 2
abf : 1
I would like overwrite my file and write result code to file:
import os
import re
folderPath = r'C:/Users/adm/test'
if os.path.exists(folderPath):
files = []
for name in os.listdir(folderPath):
if os.path.isfile(os.path.join(folderPath, name)):
files.append(os.path.join(folderPath, name))
for ii in files:
with open(ii, "r") as f:
# Create an empty dictionary
d = dict()
# Loop through each line of the file
for line in f:
# Remove the leading spaces and newline character
line = line.strip()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Split the line into words
words = line.split(" ")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] 1
else:
# Add the word to dictionary with count 1
d[word] = 1
with open(ii, "w") as outfile:
# # Print the contents of dictionary
for key in list(d.keys()):
n = print(key, ":", d[key])
outfile.write('%s:%s\n' % key, str(d[key]))
CodePudding user response:
Initially "blankpaper.txt" is
abc
cds
cds
abf
After applying the code below, "blankpaper.txt" becomes (as requested)
abc : 1
cds : 2
abf : 1
As requested, I overwrote the original file. However, I would recommend either (1) making a copy of the original before overwriting it (2) writing to another file as parsing files is not perfect if the input is not consistent. Parsing files assumes a particular format from the input and if you get similar but different input in the future, the same code may not work and you risk losing data from the original file if you overwrite it directly without first making a copy of the original.
I hope this helps. Please let me know if there are any questions or if this is not quite what you wanted!
# dictionary storing unique words and counts
words = {}
# r opens file for both reading and writing
with open("blankpaper.txt", 'r ') as f:
# read file and count words
for line in f:
# assuming one line = one word
word = line.strip()
if word in words:
words[word] = 1
else:
words[word] = 1
# clear file
f.seek(0)
f.truncate()
# write to the file
for key in words:
f.write(f"{key} : {words[key]}\n")
print(words) # -> {'abc': 1, 'cds': 2, 'abf': 1}