Python error :OSError: [Errno 9] Bad file descriptor

Question:

I have wanted to transform DOCX file using docx library. Everytime I run it i get this error

OSError: [Errno 9] Bad file descriptor

The code is :

from docx import Document


def bionify(path_to_text: str) -> None:
    doc = Document(path_to_text)
    new_doc = Document()
    all_paragraphs = doc.paragraphs
    for paragraph in all_paragraphs:
        word_list = paragraph.text.split(' ')
        new_paragraph = new_doc.add_paragraph()
        for word in word_list:
            i = 0
            while i < len(word):
                if i == 0 or i == 1:
                    new_paragraph.add_run(word[i]).bold = True
                else:
                    new_paragraph.add_run(word[i]).bold = False
                i += 1
            new_paragraph.add_run(' ')
    # Input the path to the document that you wish to save to:
    new_doc.save('sample_output.docx')


if __name__ == '__main__':
    # Input the path to the document containing your text file you wish to read from:
    bionify(r'C:Users###Desktopbionic python reader transformerBionicTexterizersample_input.docx')

I have changed the destination, python package, python version to run it. But every time I get OSError: [Errno 9] Bad file descriptor

Complete tracepack:

Traceback (most recent call last):
  File "c:Users####Desktopbionic python reader transformerBionicTexterizermain.py", line 62, in <module>
    bionify(r'C:Users####Desktopbionic python reader transformerBionicTexterizersample_input.docx')
  File "c:Users####Desktopbionic python reader transformerBionicTexterizermain.py", line 57, in bionify
    new_doc.save('sample_output.docx')
  File "C:Python310libsite-packagesdocxdocument.py", line 135, in save
    self._part.save(path_or_stream)
  File "C:Python310libsite-packagesdocxpartsdocument.py", line 111, in save
    self.package.save(path_or_stream)
  File "C:Python310libsite-packagesdocxopcpackage.py", line 172, in save
    PackageWriter.write(pkg_file, self.rels, self.parts)
  File "C:Python310libsite-packagesdocxopcpkgwriter.py", line 33, in write
    PackageWriter._write_content_types_stream(phys_writer, parts)
  File "C:Python310libsite-packagesdocxopcpkgwriter.py", line 45, in _write_content_types_stream
    phys_writer.write(CONTENT_TYPES_URI, cti.blob)
  File "C:Python310libsite-packagesdocxopcphys_pkg.py", line 155, in write
    self._zipf.writestr(pack_uri.membername, blob)
  File "C:Python310libzipfile.py", line 1810, in writestr
    with self.open(zinfo, mode='w') as dest:
  File "C:Python310libzipfile.py", line 1176, in close
    self._fileobj.seek(self._zinfo.header_offset)
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <function ZipFile.__del__ at 0x0000022D9BF4BEB0>
Traceback (most recent call last):
  File "C:Python310libzipfile.py", line 1815, in __del__
    self.close()
  File "C:Python310libzipfile.py", line 1837, in close
    self._fpclose(fp)
  File "C:Python310libzipfile.py", line 1937, in _fpclose
    fp.close()
Asked By: Bouncyknights123

||

Answers:

Windows 11. It is a problem with windows 11. I have ran the code without any problems on windows 10. There seems to some package permission issues.

Answered By: Bouncyknights123

I am on Windows 11 and turning off Controlled folder access from Windows Security worked for me. Hope that helps!

Answered By: Victor Badulescu