reading pg_dump file occurs at open the file - python

I'm using the pgdumplib lib. Unfortunately there is an error, when I'm trying to open the file. The file is in the same folder as the python script. I'm using Python 3.7
Code:
import pgdumplib
dump = pgdumplib.load('test.dump')
print('Database: {}'.format(dump.toc.dbname))
print('Archive Timestamp: {}'.format(dump.toc.timestamp))
print('Server Version: {}'.format(dump.toc.server_version))
print('Dump Version: {}'.format(dump.toc.dump_version))
for line in dump.table_data('public', 'pgbench_accounts'):
print(line)
Error:
Traceback (most recent call last):
File "C:/Users/user/data/test.py", line 3, in <module>
dump = pgdumplib.load('test.dump')
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\__init__.py", line 24, in load
return dump.Dump(converter=converter).load(filepath)
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\dump.py", line 228, in load
raise ValueError('Path {!r} does not exist'.format(path))
ValueError: Path 'test.dump' does not exist

If you are running your code from C:/Users/user/700Joach/project/ and you have the following line in your script:
dump = pgdumplib.load('test.dump')
Then, python would look for the following path to open test.dump:
C:/Users/user/700Joach/project/test.dump
Namely, this part: load('test.dump') internally is forging a relative path to test.dump.
You can do several things to resolve the issue. Either move test.dump to the directory from which you are executing your code. Or, provide an absolute path to your test.dump as follows:
dump = pgdumplib.load('C:/Users/user/700Joach/project/test.dump')

Related

Unexpected error with pdf2image while using a python loop

I'm using the pd2image module to convert a list of .ai files into .png files in a python loop. When I use the module in a loop it will successful convert the first .ai file into a .png file per page in the .ai file, but it seems to break on the second .ai file.
Here's the code
import os
from pdf2image import convert_from_path
directory = '/Users/jacobpatty/vscode_projects/badger_colors/test_ai_work_orders'
def ai_to_png(ai_file):
convert_from_path(
ai_file,
dpi=200,
output_folder='/Users/jacobpatty/vscode_projects/badger_colors/ai_to_ping_temp_storage',
fmt='png'
)
def end_loop(database):
for filename in os.scandir(database):
ai_to_png(filename)
print(filename)
end_loop(directory)
and here's the error
<DirEntry '8904_Heather_B_12-6-17.ai'>
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/pdf2image/pdf2image.py", line 479, in pdfinfo_from_path
raise ValueError
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/jacobpatty/vscode_projects/badger_colors/TS_pdf2image.py", line 21, in <module>
end_loop(directory)
File "/Users/jacobpatty/vscode_projects/badger_colors/TS_pdf2image.py", line 17, in end_loop
ai_to_png(filename)
File "/Users/jacobpatty/vscode_projects/badger_colors/TS_pdf2image.py", line 7, in ai_to_png
convert_from_path(
File "/opt/homebrew/lib/python3.9/site-packages/pdf2image/pdf2image.py", line 98, in convert_from_path
page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
File "/opt/homebrew/lib/python3.9/site-packages/pdf2image/pdf2image.py", line 488, in pdfinfo_from_path
raise PDFPageCountError(
pdf2image.exceptions.PDFPageCountError: Unable to get page count.
Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't read xref table
I am very new to programming, but it seems odd to me that a line of code would work for one iteration but not the next. To fix it I tried using different ai files, but nothing changed and the same error occurred. I tried uninstalling and reinstalling pdf2image but that also didn't help.
Any ideas?

Issue with File Not Found

I'm having an issue just drawing from a file which I don't think I've had an issue with before. I'm not sure if it's because I switched from PyCharm to IDLE
Here is my current code:
import time
import os
keep_running = True
last_time = 0
file = os.path.abspath(r'C:\Users\AUser\Desktop\test.txt')
current_time = os.path.getmtime(file)
print(file)
Here is the output I'm getting is:
Traceback (most recent call last):
File "C:/Users/AUser/Desktop/Scripts/FileAlert.py", line 9, in <module>
current_time = os.path.getmtime(file)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\AUser\\Desktop\\test.txt'
If I remove 'r' from the file path, I get a unicode error instead. The file is in a different directory from the script so I'm not sure what the issue is. This is happening with Python 3.9 on a Windows 10 machine.
In this case the issue is the file being searched for was technically named 'test.txt.txt'.
Using this suggestion helped me to locate this error on my end:
Does that file exist? It seems it does not. You could try import os and then os.listdir('C:\Users\AUser\Desktop') to get the directory list. from user #tdelaney

Load Configuration File with Juniper PyEZ

I am trying to load override a config stored on the juniper device (vSRX) with PyEZ. The code looks like this
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
dev = Device(host="x.x.x.x", user='user', password='pass').open()
with Config(dev, mode='private') as cu:
cu.load('load override minimal.conf', format='set')
cu.pdiff()
if cu.commit_check():
cu.commit()
When I run the script I get below error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/utils/config.py", line 534, in load
ignore_warning=ignore_warning)
File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/utils/config.py", line 455, in try_load
raise ConfigLoadError(cmd=err.cmd, rsp=err.rsp, errs=err.errs)
jnpr.junos.exception.ConfigLoadError: ConfigLoadError(severity: error, bad_element: load, message: error: unknown command)
Can't find a solution to my problem. Any help is greatly appreciated!
If you are trying to load config from a local file (on junos device), you should use url option
Check for url in:
http://junos-http://junos-pyez.readthedocs.io/en/2.1.8/jnpr.junos.utils.html#jnpr.junos.utils.config.Config.load
Your cu.load() is wrong.
Here is an example:
https://github.com/Juniper/junosautomation/blob/master/pyez/pyez_building_blocks/bb4.provisioning.with.pyez.py

Runtime error while trying to load a dataset as a table in Snap.py

I was trying to load co04_dist.txt from http://people.sc.fsu.edu/~jburkardt/datasets/cities/cities.html as a table in Snap.py
import snap
context = snap.TTableContext()
filename = "co04_dist.txt"
schema = snap.Schema()
schema.Add(snap.TStrTAttrPr("Col1", snap.atInt))
schema.Add(snap.TStrTAttrPr("Col2", snap.atInt))
schema.Add(snap.TStrTAttrPr("Col3", snap.atInt))
schema.Add(snap.TStrTAttrPr("Col4", snap.atInt))
table = snap.TTable.LoadSS(schema, filename, context, "\t", snap.TBool(False))
But all I get is the runtime error claiming that the application requested the runtime to terminate it in an unusual way. It also says this in the shell:
Traceback (most recent call last):
File "C:\Users\User\Documents\city.py", line 12, in <module>
table = snap.TTable.LoadSS(schema, filename, context, "\t", snap.TBool(False))
File "C:\Python27\lib\snap.py", line 23070, in LoadSS
return _snap.TTable_LoadSS(*args)
RuntimeError: Execution stopped: Ss.GetFlds() == S.Len(), file c:\cygwin\home\rok\build\snap\snap-core\table.cpp, line 683
I have already ensured that the txt file is already in the same directory as the .py file.
You need to have this specification on top of the dataset for it to run

gensim file not found error

I am executing the following line:
id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')
This code is available at "https://radimrehurek.com/gensim/wiki.html". I downloaded the wikipedia corpus and generated the required files and wiki_en_wordids.txt is one of those files. This file is available in the following location:
~/gensim/results/wiki_en
So when i execute the code mentioned above I get the following error:
Traceback (most recent call last):
File "~\Python\Python36-32\temp.py", line 5, in <module>
id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')
File "~\Python\Python36-32\lib\site-packages\gensim\corpora\dictionary.py", line 344, in load_from_text
with utils.smart_open(fname) as f:
File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 129, in smart_open
return file_smart_open(parsed_uri.uri_path, mode)
File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 613, in file_smart_open
return open(fname, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'wiki_en_wordids.txt'
Even though the file is available in the required location I get that error. Should I place the file in any other location? How do I determine what the right location is?
The code requires an absolute path here. Relative path should be used when entire operation is carried out in the same directory location, but in this case, the file name is passed as argument to some other function which is located at different location.
One way to handle this situation is using abspath -
import os
id2word = gensim.corpora.Dictionary.load_from_text(os.path.abspath('wiki_en_wordids.txt'))

Categories