0

I want to lemmatize a corpus of 24 .txt files (dir1) and save them in dir2. I want to write the output of print(word.lemma_, end=" ") in dir2. This is the code I have so far:

import os
import re
import nltk
import spacy
import sys
sp = spacy.load('en_core_web_sm')
>>> dir1=("corpus")
>>> dir2=("corpus2")
for txt in os.listdir(dir1):
    file=open(dir1+"/"+txt, "r", encoding="utf-8")
    for line in file:
        sentence=sp(line)
        for word in sentence:
            print(word.lemma_, end=" ")
martineau
  • 119,623
  • 25
  • 170
  • 301
mxrdybxm
  • 13
  • 2
  • 2
    Does this answer your question? [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – Woodford May 24 '21 at 21:45
  • 1
    The [`print()`](https://docs.python.org/3/library/functions.html#print) function has an optional *`file=`* argument that will send the output to one. In addition, the `contextlib` module has a [`redirect_stdout()`](https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout) function that can be applied to all the `print()` calls within a block of code. – martineau May 24 '21 at 22:02

0 Answers0