A
A
Artur Kirpichev2017-01-19 16:52:56
Automation
Artur Kirpichev, 2017-01-19 16:52:56

How to automatically calculate the amount of text in text documents?

System: MacOS (there is also Windows, but I would like to find a solution specifically for poppy)
Straight to the point:
We have a folder with text documents (.txt).
You need to get a new text document in which there will be a list of the titles of these text files and in front of each title the amount of text (number of characters without spaces).
For some reason, the idea immediately came to my mind about the built-in Automator process automator. But I don't know how to use it at all. Googling something like that didn't work.
Also, in the standard macOS text editor there is no way to see the amount of text, but it is solved by installing an additional "Service" (service name: WordService), it has the WordService:Statistics command with a hot key (cmd + shift + I) and a window with a character count opens at the selected text.
It is also possible that some script will be the solution, I don’t know anything about them either.
Guys, help with this task. Very necessary. There is a huge number of texts in the work, and for each you need to know the volume of characters. You can also do it manually, but it's much more interesting to automate everything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tash1moto, 2017-01-19
@letaon2

import os,re,sys
folder = sys.argv[1]
path = os.listdir(folder)
log = open(os.path.join(folder,'log.txt'),'w')
for j in path:
  if "txt" in j:
    words =0
    f=os.path.join(folder,j)
    with open(f) as file:
      for line in file:
        words+=len(re.findall('\w+', line))
    log.write(j+' = '+str(words)+' words'+'\n')
log.close()

save as doc.py, open a terminal from that folder,
in the terminal write
python doc.py "path to the folder with text files" (for example "/home/user/txtfiles")
wait...
log.txt will appear in the folder with files with records

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question