I am a beginner. I have a simple txt file which I need to read (using numpy). I have the program in the same directory as the .txt file.
I have checked the cwd and it's the right one. Also, I've written a text file in order to see if python wants to open that one - that file opens just fine.
import os
import numpy as np
np.loadtxt("test2.txt")
The code above gives me the error.
The code below works just fine.
import os
import numpy as np
x = np.array([1, 2, 3])
np.savetxt("test.txt", x)
y = np.loadtxt("test.txt")
print(y)
The error I get is:
Traceback (most recent call last):
File "D:\detest\admi.py", line 5, in <module>
np.loadtxt("test2.txt")
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\npyio.py", line 962, in loadtxt
fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\_datasource.py", line 266, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\_datasource.py", line 624, in open
raise IOError("%s not found." % path)
OSError: test2.txt not found.
Can you use the Python read file instead?
path = '' # location of your file
openfile = open(path, 'r') # open file
openfile.read() # return all content of file
openfile.close() # close file
Related
I'm learning the NumPy library and when I try to read something from the file I get this error:
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\Reading_from_file.py", line 3, in <module>
example = genfromtxt("example.txt", delimiter=',')
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\npyio.py", line 1793, in genfromtxt
fid = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\_datasource.py", line 193, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\_datasource.py", line 533, in open
raise IOError("%s not found." % path)
OSError: example.txt not found.
Here's the code:
from numpy import genfromtxt
example = genfromtxt("example.txt", delimiter=',')
Reading_from_file.py and example.txt are in the same folder
I read the documentation and I was trying to find something here but found nothing (maybe I missed something)
If there is already a thread on this topic, please link to it
You probably aren't running the script from the same folder that example.txt is in. example.txt doesn't need to be in the same directory as the script itself, it needs to be in the same directory as you are when you're running the script.
This is not exactly your scenario, but I got the same error while passing a string to genfromtxt and I was confused.
In fact, genfromtxt expects a file path by default. If you want to pass a string you need to simulate a file by wrapping the string in a StringIO object:
from io import StringIO
from numpy import genfromtxt
example = genfromtxt(StringIO("1, 3"), delimiter=',')
print(example)
Returns:
[1. 3.]
In a Folder called Assignment Parser, I've my parsing.py file along with a auth.txt file. Trying to open this auth.txt file. But getting an error that says :
(base) C:\Users\Ajay\Desktop\Python\Assignment Parser>python parsing.py
Traceback (most recent call last):
File "parsing.py", line 27, in <module>
main()
File "parsing.py", line 8, in main
file = open(file_path / "auth.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Ajay\\Desktop\\Python\\Assignment Parser\\auth.txt'
Code:
from pathlib import Path
import os
def main():
# read file
# C:\Users\Ajay\Desktop\Python\Assignment Parser\
file_path = Path("C:/Users/Ajay/Desktop/Python/Assignment Parser/")
file = open(file_path / "auth.txt","r")
# file = open("auth.txt", "r")
lines = file.readlines()
file.close()
Where is this going wrong? PFA for the screenprint.
Try this:
from pathlib import Path
import os
def main():
# read file
# C:\Users\Ajay\Desktop\Python\Assignment Parser\
file_path = Path("C:/Users/Ajay/Desktop/Python/Assignment Parser/")
file = open(os.path.join(file_path, "auth.txt"), "r")
# file = open("auth.txt", "r")
lines = file.readlines()
file.close()
I think the problem is in file extension, I see parsing has .py extension but auth is not
please try file = open(file_path / "auth", "r") again (just delete .txt extension)
As you have your python file in same folder as your text file. You can directly use below code.
def main():
file = open("./auth.txt")
lines = file.readlines()
file.close()
Also make sure, your syder working directory is set to this folder path "C:/Users/Ajay/Desktop/Python/Assignment Parser"
Dears,
I'm creating a script python to mass upload files in Plone site, the installation is UnifiedInstaller Plone 4.3.10.
This script read a txt, and this txt have separation with semicolon, the error appear when set up a file in a new created item.
Bellow the Script.
from zope.site.hooks import setSite
from plone.namedfile.file import NamedBlobFile
from plone import api
import transaction
import csv
portal = app['Plone']
setSite(portal)
container = portal['PROCESSOS']
with open('CARGA/C008_0002.txt', 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
for row in reader:
pdf_id = 'P'+str(row['IMAGEM']).strip('Pasta Geral\\ ')
file_obj = api.content.create(
container, 'File',
title=str(row['INTERESSADO']),
id=pdf_id,
description=str(row['CNPJ / CPF'])+' '+str(row['ASSUNTO']),
safe_id=True
)
pdf_path = 'INMEQ/'+str(row['IMAGEM']).replace("\\", "/")
print(pdf_path)
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=str(file_obj.id),
)
print('Created: '+row['NDOPROCESSO']+'.')
transaction.commit()
When the script will set up a file the error "WrongType" appear. See verbose bellow.
Traceback (most recent call last):
File "<console>", line 18, in <module>
File "/home/jaf/plone4310/buildout-cache/eggs/plone.namedfile-3.0.9-py2.7.egg/plone/namedfile/file.py", line 384, in __init__
self.filename = filename
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/fieldproperty.py", line 52, in __set__
field.validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 182, in validate
self._validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 309, in _validate
super(MinMaxLen, self)._validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 209, in _validate
raise WrongType(value, self._type, self.__name__)
WrongType: ('processo-al-1.pdf', <type 'unicode'>, 'filename')
Thanks for you attention!
--
Juliano Araújo
You need to pass the filename as unicode.
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=unicode(file_obj.id), # needs to be unicode
)
More Info in the plone.namedfile docu --> https://github.com/plone/plone.namedfile/blob/36014d67c3befacfe3a058f1d3d99a6a4352a31f/plone/namedfile/usage.rst
I am using Pandas on Mac, to read and write a CSV file, and the weird thing is when using full path, it has error and when using just a file name, it works. I post my code which works and which not works in my comments below, and also detailed error messages. Anyone have any good ideas?
sourceDf = pd.read_csv(path_to_csv)
sourceDf['nameFull'] = sourceDf['nameFirst'] + ' ' + sourceDf['nameLast']
sourceDf.to_csv('newMaster.csv') # working
sourceDf.to_csv('~/Downloads/newMaster.csv') # not working
Traceback (most recent call last):
File "/Users/foo/PycharmProjects/DataWranglingTest/CSVTest1.py", line 36, in <module>
add_full_name(path_to_csv, path_to_new_csv)
File "/Users/foo/PycharmProjects/DataWranglingTest/CSVTest1.py", line 28, in add_full_name
sourceDf.to_csv('~/Downloads/newMaster.csv')
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 1189, in to_csv
formatter.save()
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/format.py", line 1442, in save
encoding=self.encoding)
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/common.py", line 2831, in _get_handle
f = open(path, mode)
IOError: [Errno 2] No such file or directory: '~/Downloads/newMaster.csv'
Tried to use prefix r, but not working,
path_to_csv = r'~/Downloads/Master.csv'
path_to_new_csv = r'~/Downloads/Master_new.csv'
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 1189, in to_csv
formatter.save()
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/format.py", line 1442, in save
encoding=self.encoding)
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/common.py", line 2831, in _get_handle
f = open(path, mode)
IOError: [Errno 2] No such file or directory: '~/Downloads/Master_new.csv'
thanks in advance,
Lin
Try using os.path.join().
import os
(...)
output_filename = 'newMaster.csv'
output_path = os.path.join('Downloads', output_filename)
(...)
sourceDf.to_csv(output_path)
Use the same methodology to point pandas.read_csv() in the right direction.
You didn't specify python version.
On 3.4 you can use pathlib, otherwise use os.path.join() or quoting:
sourceDf.to_csv(r'~/Downloads/newMaster.csv')
Notice the r.
The problem is that /n is newline, which is not allowed in a path.
My task gets some files with header + image content. After the header extraction and create the image.png which is recognized and properly opened. This program works on windows with python 2.7.9 and the latest version at the time of PIL
Afterwords the image is converted from png to jpeg.
The code snippet:
im = Image.open("c:\\1\\rawfile.png")
im.save('c:\\1\\rawfile.jpeg',"JPEG")
Here appears the error (on the im.save() line), it only after the loading, if i do img.crop(x), img.rotate(x) the same error appears.
Traceback (most recent call last):
File "getMail.py", line 225, in <module>
start_deamon()
File "getMail.py", line 217, in start_deamon
deamon.process_email()
File "getMail.py", line 114, in process_email
self.img_conv.convert_file('c:\\1\\rawfile\\rawfile.png', 'c:\\1\\rawfile\\rawfile.jpg' )
File "getMail.py", line 162, in convert_file
im.save('c:\\1\\rawfile.jpeg',"JPEG")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 198, in load
s = read(self.decodermaxblock)
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 391, in load_read
cid, pos, len = self.png.read()
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 96, in read
len = i32(s)
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 44, in i32
return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
IndexError: string index out of range
I've tried the LOAD_TRUNCATED_IMAGES set to YES and it didn't work. I've also tried absolute paths with no luck.
On a debug stand alone program using the same hardcoded paths on the same files it works! (files are created, converted and properly read by file editors)
try:
with open( 'c:\\1\\rawFile', 'rb') as fin:
data = fin.read()
fin.close()
except:
print 'error1'
#do my stuff here
try:
with open( 'c:\\1\\rawfile.png', 'wb') as fout:
fout.write(data[index:])
fout.close()
except:
print 'error2'
try:
Image.open('c:\\1\\rawfile.png').save('c:\\1\\rawfile.jpg')
except:
print 'error 3'
If I hardcode the file paths on the main project it will fail and give the IndexError.
The original PIL was being used. Instead I've upgraded to Pillow (a PIL fork) which with the following code solved the problem. (as already described)
from PIL import ImageFile
#To ensure that *.png file are read
ImageFile.LOAD_TRUNCATED_IMAGES = True