How to read a large file set - python

I am very new to Python. So please give specific advice. I am using Python 3.2.2.
I need to read a large file set in my computer. Now I can not even open it. To verify the directory of the file, I used:
>>> import os
>>> os.path.dirname(os.path.realpath('a9000006.txt'))
It gives me the location 'C:\\Python32'
Then I wrote up codes to open it:
>>> file=open('C:\\Python32\a9000006.txt','r')
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
file=open('C:\\Python32\a9000006.txt','r')
IOError: [Errno 22] Invalid argument: 'C:\\Python32\x079000006.txt'
Then I tried another one:
>>> file=open('C:\\Python32\\a9000006.txt','r')
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
file=open('C:\\Python32\\a9000006.txt','r')
IOError: [Errno 2] No such file or directory: 'C:\\Python32\\a9000006.txt'
Then another one:
>>> file=open(r'C:\Python32\a9000006.txt','r')
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
file=open(r'C:\Python32\a9000006.txt','r')
IOError: [Errno 2] No such file or directory: 'C:\\Python32\\a9000006.txt'
The file is saved in the Python folder. But, it is in a folder, so the path is D\Software\Python\Python3.2.2\Part1\Part1awards_1990\awd_1990_00. It is multiple layers of folders.
Also, and anyone share how to read the abstract section of all files in that folder? Thanks.

\a is the ASCII bell character, not a backslash and an a. Use forward slashes instead of backslashes:
open('C:/Python32/a9000006.txt')
and use the actual path to the file instead of C:/Python32/a9000006.txt It's not clear from your question what that path might be; you seem like you might already know the path, but you're misusing realpath in a way that seems like you're trying to use it to search for the file. realpath doesn't do that.

Related

PyPDF2: writing output to stdout fails with python3

I am trying to use Python 3.7.2 with PyPDF2 1.26 to select some pages of an input PDF file and write the output to stdout (the actual code is more complicated, this is just a MCVE):
import sys
from PyPDF2 import PdfFileReader, PdfFileWriter
input = PdfFileReader("example.pdf")
output = PdfFileWriter()
output.addPage(input.getPage(0))
output.write(sys.stdout)
This fails with the following error:
UserWarning: File <<stdout>> to write to is not in binary mode. It may not be written to correctly. [pdf.py:453]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 487, in write
stream.write(self._header + b_("\n"))
TypeError: write() argument must be str, not bytes
The problem seems to be that sys.stdout is not open in binary mode. As some of the answers suggest, I have tried the following:
output.write(sys.stdout.buffer)
This fails with the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 491, in write
object_positions.append(stream.tell())
OSError: [Errno 29] Illegal seek
I have also tried the answer from Changing the way stdin/stdout is opened in Python 3:
sout = open(sys.stdout.fileno(), "wb")
output.write(sout)
This fails with the same error as above.
How can I use the PyPDF2 library to output a PDF to standard output?
More generally, how do I correctly switch sys.stdout to binary mode (akin to Perl's binmode STDOUT)?
Note: There is no need to tell me that I can open a file in binary mode and write the PDF to that file. That works; however, I specifically want to write the PDF to stdout.
From the documentation:
write(stream)
Writes the collection of pages added to this object out as a PDF file.
Parameters: stream – An object to write the file to. The object must support the write method and the tell method, similar to a file object.
It turns out that sys.stdout.buffer is not tellable if not redirected to a file, hence you can't use it as a stream for PdfFileWriter.write.
Say your script is called myscript. If you call just myscript, then you'll get this error, but if you use it with a redirection, as in:
myscript > myfile.pdf
then Python understands it's a seekable stream, and you won't get the error.

How to fix this error while executing a program

C:\Users\skandregula\AppData\Local\Programs\Python\Python37\python.exe
C:/Users/skandregula/Desktop/TestFiles2/testing.py
Traceback (most recent call last):
File "C:/Users/skandregula/Desktop/TestFiles2/testing.py", line 30, in <module>
with open(in_dir + f, 'r') as tmp_file:
FileNotFoundError: [Errno 2] No such file or directory:
'C:/Users/skandregula/Desktop/TestFiles2/history.log.3.3C'
Process finished with exit code 1
that is the error i am running right now...How to fix this?
Without seeing what your code looks like, I'm assuming somewhere in your code you're referencing a file or folder named 'history.log.3.3C' as the error suggests, and that it cannot find it in your directory given. My best advice is make sure the directory is correct?
Hard to tell without seeing what you're trying to do. Maybe posting a snippet of your code would help.

Trying to use '\' with 'os.startfile' in python, when taking variables from excel using xlrd

I am having issues trying to open jpg files with python, when I'm taking there names from excel.
Here's what I have so far in code:
print(worksheet.cell(rowidx,2))
image = worksheet.cell(rowidx,11)
print(image)
image_name = str(image).replace("'","")
print(image_name)
image_name2 = image_name.replace("text:","")
print(image_name2)
os.startfile(image_name2)
I used the print function to help me debug.
Whenever an input goes in, for this example let's say "d:\cow.jpg", the following output is displayed:
text:'d:\\cow.jpg'
text:d:\\cow.jpg
d:\\cow.jpg
Traceback (most recent call last)
File "<pyshell#2>", line 1, in <module>
generate(1, all, all, all, all, 1, 1, "Murloc")
File "C:\Users\Alexander\Dropbox\Documents\Python\Random Hearthstone Card
Gen\Random Hearthstone Card Generator.py", line 38, in generate
os.startfile(image_name2)
FileNotFoundError: [WinError 15] The system cannot find the drive specified:
'd:\\\\cow.jpg'
I have solved it!
I just changed the cell type from general to text, and replaced the \ with a \\.

Is this the right way to use open() in python? its showing an error

Using the code below:
txt = open("/data/cards/deckofcards.txt").read().splitlines()
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/data/cards/deckofcards.txt'
txt=open("/home/vamsi/data/cards/deckofcards.txt").read().splitlines()
print(v)
I missed the (/home/vamsi) path.I misunderstood that pwd is by default and no need to give it in open().I figured it out and it is working fine.in open() we should also give pwd also to open the (.txt,.tsv etc..)file as we needed.
thank you,
screen shot of terminal
Give the absolute path of the file.
It would be something like C:/Desktop/data/card.txt.
The directory you mentioned does not exist and hence it is giving you that error.

Error when using Matplotlib.image in Python

Okay, so I just started this tutorial using Anaconda in PyCharm. I imported the correct libraries, but when I try to upload my image using this code:
img = mpimg.imread('FileName.png')
I get this error message:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File ".../matplotlib/image.py", line 1323, in imread
with open(fname, 'rb') as fd:
IOError: [Errno 2] No such file or directory: 'FileName.png'
I tried putting in the file location like this:
img=mpimg.imread('FilePath/FileName.png')
But I got the same error. I want to follow the tutorial exactly so I'm not sure why this isn't working! Any help is appreciated!
Make sure that there is that file in the folder...
Try this command to list the files in that folder just to make sure that python atleast recognizes or reads the files.
import os
print (os.listdir('your path'))
you should give an absolute path for the image. for example
r'C:\Users\HP\Desktop\stinkbug.png' adding r to remove the Unicode error
I was providing the full path: "C:/user/username/Downloads/folder/file.ext"
When I changed it to relative, it worked for me: "Downloads/folder/file.ext"

Categories