Redirect Output Of A Function That Converts Pdf To Txt Files To A New Folder In Python
Solution 1:
you can use os.path,join, you have to give your directory path and filename with extension. it will create a full url and creates a file. You can use it like below
with open(os.path.join(dir_path,fileCompleteName), "w") as file1:
file1.write("Hello World")
In windows any of the below should work
"D:/extracted_text/"
os.path.join("/", "D:", "extracted_text", outfile)
os.path.join("D:/", "extracted_text", outfile)
Make sure directory path is exist "D:/extracted_text"
Solution 2:
The problem lies in line:
outfile = os.path.splitext(os.path.abspath(fname))[0] + '.txt'
If you print out outfile, you'll see that it contains the full path of your file. Replace it with:
outfile = os.path.splitext(fname)[0] + '.txt'
This should solve your problem! Note that this will break if 'D:/extracted_text/' does not exist. So either create that directory manually or programmatically using os.makedir
.
EDIT: To break down the problem into smaller pieces, open a new file and run this snippet, see if it does the trick, then make the changes in the original code:
import os
fname = "some_file.pdf"
text = "Here's the extracted text"
savepath = 'D:/extracted_text/'
outfile = os.path.splitext(fname)[0] + '.txt'
print(outfile)
comp_name = os.path.join(savepath,outfile)
print(comp_name)
with open(comp_name, 'w', encoding = 'utf-8') as pdf_file:
pdf_file.write(text)
Post a Comment for "Redirect Output Of A Function That Converts Pdf To Txt Files To A New Folder In Python"