How to get content of file inside package - python

I am writing something in Python where I want to use predefined texts from files within the package. Somehow I can't manage to get it to work in Eclipse PyDev Console.
This is my path structure. From "story.py" I want to use the content of "starttext".
I tried open() with multiple variations of os.getcwd() and os.path.dirname(sys.argv[0]) which resulted in
FileNotFoundError: [Errno 2] No such file or directory: '..\starttext'
My last attempt was trying something like
import pkg_resources
resource_package = __name__
resource_path = '/'.join(('.', 'starttext'))
template = pkg_resources.resource_stream(resource_package, resource_path)
resulting in:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\Python\Python36-64\lib\site-packages\pkg_resources\__init__.py", line 1232, in resource_stream
self, resource_name
File "C:\Program Files\Python\Python36-64\lib\site-packages\pkg_resources\__init__.py", line 1479, in get_resource_stream
return io.BytesIO(self.get_resource_string(manager, resource_name))
File "C:\Program Files\Python\Python36-64\lib\site-packages\pkg_resources\__init__.py", line 1482, in get_resource_string
return self._get(self._fn(self.module_path, resource_name))
File "C:\Program Files\Python\Python36-64\lib\site-packages\pkg_resources\__init__.py", line 1560, in _get
"Can't perform this operation for loaders without 'get_data()'"
NotImplementedError: Can't perform this operation for loaders without 'get_data()'
which appears to have something to do with python 3.x?
This seems to be such an easy task and I don't understand whats wrong.
Any help is appreciated.
Thank you.
update
Thanks to ShmulikA I changed it to:
from os.path import dirname, join, abspath
filename = join(dirname(abspath(communitybot.anthology.teststory.story.__file__)), 'starttext')
file = open(filename, 'r')
content = file.read()
This works although I think it is a little bit long, but I'm certain I am still doing something wrong there.

seems like you are missing a \ - use os.path.join:
from os.path import dirname, join, abspath
filename = join(dirname(abspath(__file__)), 'starttext')
file = open(filename, 'r')
__file__ - the path to the module's source file (you can also do import requests;requests.__file__)
os.path.abspath - returns the absolute filename (e.g. abspath('..') returns /home)
os.path.dirname - returns the dirname of a file
os.path.join - joins a file parts compatible on both linux and windows

Related

No such file or directory: 'GoogleNews-vectors-negative300.bin'

I have this code :
import gensim
filename = 'GoogleNews-vectors-negative300.bin'
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
and this is my folder organization thing :
image of my folder tree that shows that the .bin file is in the same directory as the file calling it, the file being ai_functions
But sadly I'm not sure why I'm having an error saying that it can't find it. Btw I checked, I am sure the file is not corrupted. Any thoughts?
Full traceback :
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/run.py", line 1, in <module>
from serv import app
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/__init__.py", line 13, in <module>
from serv import routes
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/routes.py", line 7, in <module>
from serv.ai_functions import checkplagiarism
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/ai_functions.py", line 31, in <module>
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1629, in load_word2vec_format
return _load_word2vec_format(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1955, in _load_word2vec_format
with utils.open(fname, 'rb') as fin:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 188, in open
fobj = _shortcut_open(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 361, in _shortcut_open
return _builtin_open(local_path, mode, buffering=buffering, **open_kwargs)
FileNotFoundError: [Errno 2] No such file or directory: 'GoogleNews-vectors-negative300.bin'
The 'current working directory' that the Python process will consider active, and thus will use as the expected location for your plain relative filename GoogleNews-vectors-negative300.bin, will depend on how you launched Flask.
You could print out the directory to be sure – see some ways at How do you properly determine the current script directory? – but I suspect it may just be the /Users/Ile-Maurice/Desktop/Flask/flaskapp/ directory.
If so, you could relatively-reference your file with the path relative to the above directory...
serv/GoogleNews-vectors-negative300.bin
...or you could use a full 'absolute' path...
/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/GoogleNews-vectors-negative300.bin
...or you could move the file up to its parent directory, so that it is alonside your Flask run.py.

I am having an error on accessing a file from another directory with Python

This is my Python code:
from plugin import Plugin
import logging
import yaml
log = logging.getLogger('discord')
def get_bot_prefix():
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
prefixValue = yaml.safe_load(prefixReader)
prefixString = prefixValue['settings']['prefix']
return prefixString
prefix = get_bot_prefix()
However I got an error with the file accessing:
Traceback (most recent call last):
File "C:/Users/85251/Documents/Discord Bots/Python/HarTex/bot.py", line 20, in <module>
from plugins.help import Help
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 30, in <module>
prefix = get_bot_prefix()
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 22, in get_bot_prefix
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
FileNotFoundError: [Errno 2] No such file or directory: 'HarTex/hartexConfig.yaml'
How can I fix it? Or am I completely wrong with the directory?
The script should work if you are calling it from the parent directory of HarTex, maybe you are running it from a different working directory?
You could also try to open the file using the full path, as this is probably easy to check.
Error is very clear.You should use absolute path instead of relative path.
For an example home/Prakash/Desktop/test12/test.yaml
Your code definitely work,Once you will change path like this.

How to turn a comma seperated value TXT into a CSV for machine learning

How do I turn this format of TXT file into a CSV file?
Date,Open,high,low,close
1/1/2017,1,2,1,2
1/2/2017,2,3,2,3
1/3/2017,3,4,3,4
I am sure you can understand? It already has the comma -eparated values.
I tried using numpy.
>>> import numpy as np
>>> table = np.genfromtxt("171028 A.txt", comments="%")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Smith\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1551, in genfromtxt
fhd = iter(np.lib._datasource.open(fname, 'rb'))
File "C:\Users\Smith\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 151, in open
return ds.open(path, mode)
File "C:\Users\Smith\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 501, in open
raise IOError("%s not found." % path)
OSError: 171028 A.txt not found.
I have (S&P) 500 txt files to do this with.
You can use csv module. You can find more information here.
import csv
txt_file = 'mytext.txt'
csv_file = 'mycsv.csv'
in_txt = csv.reader(open(txt_file, "r"), delimiter=',')
out_csv = csv.writer(open(csv_file, 'w+'))
out_csv.writerows(in_txt)
Per #dclarke's comment, check the directory from which you run the code. As you coded the call, the file must be in that directory. When I have it there, the code runs without error (although the resulting table is a single line with four nan values). When I move the file elsewhere, I reproduce your error quite nicely.
Either move the file to be local, add a local link to the file, or change the file name in your program to use the proper path to the file (either relative or absolute).

Parse XML Tag value for all files in directory using Python

I can't quite make the leap despite pre-existing similar questions. Help would be valued!
I am trying to recursively parse all xml files in the directory/sub directory
I am looking for the value that appears for the tag "Operator id"
Example source XML:
<Operators>
<Operator id="OId_LD">
<OperatorCode>LD</OperatorCode>
<OperatorShortName>ARRIVA THE SHIRES LIMIT</OperatorShortName>
This is the code I have thus far:
from xml.dom.minidom import parse
import os
def jarv(target_folder):
for root,dirs,files in os.walk(target_folder):
for targetfile in files:
if targetfile.endswith(".xml"):
print targetfile
dom=parse(targetfile)
name = dom.getElementsByTagName('Operator_id')
print name[0].firstChild.nodeValue
This is the terminal command I am running:
python -c "execfile('xml_tag.py'); jarv('/Users/admin/Projects/AtoB_GTFS')"
And this is the error I receive:
tfl_64-31_-37434-y05.xml
encodings.xml
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "xml_tag.py", line 8, in jarv
dom=parse(targetfile)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 922, in parse
fp = open(file, 'rb')
IOError: [Errno 2] No such file or directory: 'encodings.xml'
(frigo)andytmac:AtoB_GTFS admin$ python -c "execfile('xml_tag.py'); jarv('/Users/admin/Projects/AtoB_GTFS')"
tfl_64-31_-37434-y05.xml
If I comment out the code after the 'print target file' line it does list all the xml files I have.
Thanks for your assistance,
Andy
You're not looking at the right place (relative path) : when you use for root, dirs, files in os.walk(target_folder):, files is a list of the file names in the directory root, and not their absolute path.
Try remplacing dom=parse(targetfile) by dom = parse(os.sep.join(root, targetfile))

How to use the dir/s command in Python?

Background
I use the command dir/s in batch files all the time. But, I am unable to call this using python. NOTE: I am using Python 2.7.3.
Code
import subprocess
subprocess.call(["dir/s"])
Error Message
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
subprocess.call(["dir/s"])
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I have tried changing the quotations but nothing has worked.
How would I call the dir/s module using subprocess?
How about
subprocess.call("dir/s", shell=True)
Not verified.
This is a lot different than what you're asking but it solves the same problem. Additionally, it solves it in a pythonic, multiplatform way:
import fnmatch
import os
def recglob(directory, ext):
l = []
for root, dirnames, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, ext):
l.append(os.path.join(root, filename))
return l
You need a space between dir and /s. So break it into an array of 2 elements. Also as carlosdoc pointed out, you would need to add shell=True, since the dir command is a shell builtin.
import subprocess
subprocess.call(["dir", "/s"], shell=True)
But if you're trying to get a directory listing, make it OS independent by using the functions available in the os module such as os.listdir(), os.chdir()
I finally found the answer. To list all directories in a directory (e.g. D:\\, C:\\) on needs to first import the os module.
import os
Then, they need to say that they want to list everything. Within that, they need to make sure that the output is printed.
for top, dirs, files in os.walk('D:\\'):
for nm in files:
print os.path.join(top, nm)
That was how I was able to solve it. Thanks to this.
As it's an inbuilt part of the command line you need to run it as:
import subprocess
subprocess.call("cmd /c dir /s")

Categories

Resources