Python Forum
Why doesn't this code work? What is wrong with path?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't this code work? What is wrong with path?
#1
hello, I have an error on the code below. Ths code converts all txt files into docx, from the same folder. But I have an error on path, I try many combinations, how to write the path, but still doesn't work.

This is the code:

# pip install docx
# pip install document
# pip install python-docx

from docx import Document
import re
import os

path = r'd:\2022_12_02'
direct = os.listdir(path)
print(path)

for i in direct:
    document = Document()
    document.add_heading(i, 0)
    myfile = open('d:\2022_12_02'+i).read()
    myfile = re.sub(r'[^\x00-\x7F]+|\x0c',' ', myfile) # remove all non-XML-compatible characters
    p = document.add_paragraph(myfile)
    document.save('d:\2022_12_02'+i+'.docx')
This is the error:

Error:
Traceback (most recent call last): File "D:\convert txt to docs.py", line 16, in <module> myfile = open('d:\2022_12_02'+i).read() FileNotFoundError: [Errno 2] No such file or directory: 'd:\x822_12_025g.txt' >>>
[Image: A5MjLh.jpg]

Can anyone test the code, and help me with a solution?
Reply


Messages In This Thread
Why doesn't this code work? What is wrong with path? - by Melcu54 - Jan-29-2023, 09:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 812 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 504 Nov-07-2023, 04:32 PM
Last Post: snippsat
  newbie question - can't make code work tronic72 2 709 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why wont this path work one way, but will the other way? cubangt 2 692 Sep-01-2023, 04:14 PM
Last Post: cubangt
  Something wrong with my code FabianPruitt 5 876 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 972 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 734 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 839 May-19-2023, 11:19 AM
Last Post: deanhystad
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,592 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,283 Feb-24-2023, 06:14 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020