I am doing a assignment and I need to extract text from PDF using PyPDF2 and while trying to do that am getting this error. How to fix this?
can someone help me? thank you in advance.
import PyPDF2
textFile = open('foo.txt', 'w')
file = open('foo.pdf','rb')
readpdf = PyPDF2.PdfFileReader(file)
print(readpdf.getNumPages())
1
read_pdf = readpdf.getPage(0)
textFile.write(read_pdf.extractText())
--------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
<ipython-input-42-5a892ea3012b> in <module>
----> 1 textFile.write(read_pdf.extractText())
ValueError: I/O operation on closed file.
file.close
textFile.close()
I am not sure how you ended up with this error, but this might help:
textFile = open('foo.txt', 'w')
read_pdf = readpdf.getPage(0)
textFile.write(read_pdf.extractText())
Opening the file right before you do something with it seems to work for me, so give it a try and we'll see ;]
using with open you dont need to handle the exception and closing file, it handle this by itself
import PyPDF2
with open('foo.txt','w') textFile:
with open('foo.pdf','rb') as file:
readpdf = PyPDF2.PdfFileReader(file)
print(readpdf.getNumPages())
read_pdf = readpdf.getPage(0)
textFile.write(read_pdf.extractText())
Related
I'm having issues with code that used to work during weeks.
The problem comes from this part of my code:
TypeError: ifile = open('0_Inputs/CompaniesList.csv', "r", encoding = 'utf-8')
I got the following message:
open() got an unexpected keyword argument 'encoding'
If i try:
ifile = open('0_Inputs/CompaniesList.csv', "r")
then i have an other error:
OSError: cannot identify image file '0_Inputs/CompaniesList.csv'
Im doing from PyPDF2 import PdfFileReader, PdfFileWriter but I don't if there's a conflict between libraries?
Thank you!
You should use the csv module instead of opening the file this way.
with open('0_Inputs/CompaniesList.csv', newline='') as ifile:
pass
More information: https://docs.python.org/3/library/csv.html
I am observing a "Bad address" OSError when reading from a file handle created using the with syntax in Python 3.
The file in question is 39G, but I should have enough RAM available to read the whole file. The error message leads me to believe I am hitting some kind of OS restriction; I am running CentOS 6.9. Can anyone help me understand what might be causing this behavior?
The file is perfectly readable outside of python, e.g. in bash with head or vim.
Simplified code sample producing the error is shown below:
In [2]: with open(filename, 'r', encoding="utf8") as infile:
...: infile.read()
...:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-2-3f216811bec7> in <module>()
1 with open(filename, 'r', encoding="utf8") as infile:
----> 2 infile.read()
3
OSError: [Errno 14] Bad address
I am following a tutorial to learn to read and write to a file.
I am getting the following error. I do not understand why.
C:\Python27\python.exe "C:/Automation/Python/Write to files/test3.py"
Traceback (most recent call last):
File "C:/Automation/Python/Write to files/test3.py", line 8, in <module>
f.read('newfile.txt', 'r')
ValueError: I/O operation on closed file
My code is
f = open("newfile.txt", "w")
f.write("hello world\n")
f.write("Another line\n")
f.close()
f.read('newfile.txt', 'r')
print f.read()
I have tried to put f.close at the bottom of the code but I still get the same error.
The write part works if I comment out the f.read. It is failing on the f.read part.
The line after f.close() that is f.read('newfile.txt', 'r') should be f = open('newfile.txt', 'r').
That is
f = open('newfile.txt', 'r')
print f.read()
f.close()
After which you need to add f.close() again.
Small Note
As in Python, the default value for 2nd arg of open is r, you can simple do open('newfile.txt')
You can't perform I/O operation on file_obj after closing it i.e.
file_obj.close()
So if you want to open the same file do:
if(file_obj.closed):
file_obj = open(file_obj.name, file_obj.mode)
print (file.obj.read())
file_obj.close()
As illustrated above when you closed the file you need to open your file so that you can read it
f = open('newfile.txt', 'r')
print f.read()
f.close()
I am totally new to python.
I was trying to read a file which I already created but getting the below error
File "C:/Python25/Test scripts/Readfile.py", line 1, in <module>
filename = open('C:\Python25\Test scripts\newfile','r')
IOError: [Errno 2] No such file or directory: 'C:\\Python25\\Test scripts\newfile
My code:
filename = open('C:\Python25\Test scripts\newfile','r')
print filename.read()
Also I tried
filename = open('C:\\Python25\\Test scripts\\newfile','r')
print filename.read()
But same errors I am getting.
Try:
fpath = r'C:\Python25\Test scripts\newfile'
if not os.path.exists(fpath):
print 'File does not exist'
return
with open(fpath, 'r') as src:
src.read()
First you validate that file, that it exists.
Then you open it. With wrapper is more usefull, it closes your file, after you finish reading. So you will not stuck with many open descriptors.
I think you're probably having this issue because you didn't include the full filename.
You should try:
filename = open('C:\Python25\Test scripts\newfile.txt','r')
print filename.read()
*Also if you're running this python file in the same location as the target file your are opening, you don't need to give the full directory, you can just call:
filename = open(newfile.txt
I had the same problem. Here's how I got it right.
your code:
filename = open('C:\\Python25\\Test scripts\\newfile','r')
print filename.read()
Try this:
with open('C:\\Python25\\Test scripts\\newfile') as myfile:
print(myfile.read())
Hope it helps.
I am using VS code. If I am not using dent it would not work for the print line. So try to have the format right then you will see the magic.
with open("mytest.txt") as myfile:
print(myfile.read())
or without format like this:
hellofile=open('mytest.txt', 'r')
print(hellofile.read())
I've been trying to create a python script that edits a file, but if the file is not already there, it has an error like this:
Traceback (most recent call last):
File "openorcreatfile.py", line 56, in <module>
fileHandle = (pathToFile, 'w')
IOError: [Errno 2] No such file or directory: '/home/me/The_File.txt'
It works fine if the file exists. I've also tried this:
fileHandle = (pathToFile, 'w+')
But it comes up with the same error. Do I need to explicitly check if the file is there? If so, how do I create the file?
EDIT: Sorry, I realized the folder was missing. I'm an idiot.
The error says "No such file or directory."
Since you're trying to create a file, that must not be what's missing. So you need to create the /home/me/ directory.
See os.makedirs.
fo = open("myfile.txt", "wb")
fo.write('blah')
fo.close()
That's it, this will do the job.
myfile = open('test.txt','w')
myfile.write("This is my first text file written in python\n")
myfile.close()
To check if the file is there you can do:
import os.path
os.path.isfile(pathToFile)
so you can handle it, only if it exists:
if os.path.isfile(pathToFile):
fileHandle = (pathToFile, 'w')
else:
pass #or other thing
There are several ways to create a file in python, but if you want to create a text file, take a look at numpy.savetxt, which I think is one of the easiest and most effective ways
with open("filename.txt", "w") as f:
f.write("test")