i was able to connect to an external db but while trying to generate a qr code from the result i'm getting this error.
import pymssql, pandas as pd, openpyxl, pyqrcode, os
QRCode(content=b'0001-87L58/1', error='H', version=2, mode='alphanumeric')
Traceback (most recent call last):
File "C:/Users/MSE/PycharmProjects/qrcode_gen/CIN_from_Server.py", line 25, in <module>
qr.eps(PIN +'.eps', scale=2.5, module_color='#36C')
File "C:\Users\MSE\PycharmProjects\Python4everyone\venv\lib\site-packages\pyqrcode\__init__.py", line 624, in eps
background, quiet_zone)
File "C:\Users\MSE\PycharmProjects\Python4everyone\venv\lib\site-packages\pyqrcode\builder.py", line 1461, in _eps
f, autoclose = _get_writable(file_or_path, 'w')
File "C:\Users\MSE\PycharmProjects\Python4everyone\venv\lib\site-packages\pyqrcode\builder.py", line 924, in _get_writable
stream_or_path = open(stream_or_path, mode)
FileNotFoundError: [Errno 2] No such file or directory: '0001-87L58/1.eps'
My code:
import pymssql, pandas as pd, openpyxl, pyqrcode, os
#Connecting to Db using Pymssql library
host = "myHost"
username = "Mine"
password = "mine#$"
database = "external"
conn = pymssql.connect(host, username, password, database) #connecting string
read_query = " Select * FROM dbo.M_tblPIN WHERE TI = 'A01' "
mycursor = conn.cursor()
mycursor.execute(read_query)
result = mycursor.fetchall()
for item in result:
PIN = item[12]
# using pyqrcode to create the barcode
qr = pyqrcode.create(PIN)
print(qr)
qr.eps(PIN + '.eps', scale=5, module_color='#36C')
qr.eps(PIN + '.eps', background='#eee')
How to generate the qrcode and save it in an external directory?
last line in traceback is this:
FileNotFoundError: [Errno 2] No such file or directory: '0001-87L58/1.eps'
so the item[12] contains not only a filename, but also a directory name, which does not exist. As such Python is unable to write a file into non-existing directory, and raises an exception. Either create the directory first, or be sure you're in the directory where you think the subdirectory is.
Related
I am trying to create an array of .jpg files, but the compiler is not building the array
More specifically, my problem is that a public folder, whose path is defined as the object path, is not accessible by my Python compiler [Spyder]. However, the folder, and its respective files are all public and open access to everyone. What might be the reason that my computer cannot access the images?
Code 1 is an simple function to find and access the file path I want, and the Kernal results show what is failing.
Code 2 is the syntax for the isolated error in the program I am applying the open() method. Kernal results depict compiler failure.
Code 1:
import os
path = r'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
try:
os.path.exists(path)
if (True):
R = open(path)
R.close()
except FileNotFoundError:
print("file does not exist")
Kernal for Code 1:
!runfile('C:/Users/BeckerLab/untitled6.py', wdir='C:/Users/BeckerLab')
Traceback (most recent call last):
File "C:\Users\BeckerLab\untitled6.py", line 8, in <module>
R = open(path)
PermissionError: [Errno 13] Permission denied: 'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
Code 2:
import os
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
Kernal Results for Code 2:
!runfile('C:/Users/BeckerLab/almost.py', wdir='C:/Users/BeckerLab')
2020-04-05 12:59:28
Traceback (most recent call last):
File "C:\Users\BeckerLab\almost.py", line 46, in <module>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
File "C:\Users\BeckerLab\almost.py", line 46, in <listcomp>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
FileNotFoundError: [Errno 2] No such file or directory: 'R1.JPG'
Notice that your condition is:
os.path.exists(path)
if (True):
which will always be true. Maybe try:
if (os.path.exists(path)):
Try moving the files to another directory like 'D:/../BeckerLab/untitled6.py'
I am trying to open an FDB file, but it doesn't seem to work. Whatever I try, I get this error:
Opening ./20190401_database.fdb
Traceback (most recent call last):
File "test.py", line 16, in <module>
conn = fdb.connect(dsn=local_copy, user='****', password='****', charset='iso8859_1')
File "/usr/local/lib/python3.6/site-packages/fdb/fbcore.py", line 848, in connect
"Error while connecting to database:")
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -551\n- no permission for read-write access to database /var/www/wsgi/data/20190401_database.fdb', -551, 335544352)
I am running this code as user apache, and the same user just copied the FDB file into that location, so I am pretty sure the user has read/write access.
import fdb
from datetime import date, timedelta
DB_PATH="."
yesterday = date.today() - timedelta(days = 1)
yesterday = yesterday.strftime("%Y%m%d")
filename = f'{yesterday}_database.fdb'
local_fullpath = f'{DB_PATH}/{filename}'
local_copy = local_fullpath
print("Opening "+local_copy)
conn = fdb.connect(dsn=local_copy, user='****', password='****', charset='iso8859_1')
conn.close()
Even when running the script as root, I get the same error.
The problem was in the fact that the FDB file has to be read/writable by the firebird user.
Setting that correctly made it work.
I am trying to load remote server certificate and save it in my local disk. This is the python script I'm using:
from M2Crypto.X509 import FORMAT_PEM
import StringIO
import traceback
from M2Crypto.Err import SSLError
import ssl
import socket
import pprint
import M2Crypto
from M2Crypto import X509, RSA
from datetime import datetime
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
port = 443
host='216.58.212.67' #google
#creating ssl socket
ssock = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=host)
#ssl connection
try:
ssock.connect((host, port))
except socket.error: #if tls connection is not possible
print "Faile connection with: " + host
#get the certificate
cert = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(cert)
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
When I run it, I get this error:
Traceback (most recent call last):
File "C:/Users/ealas/PycharmProjects/tlsScan/test.py", line 36, in <module>
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
File "C:\Python27\lib\site-packages\M2Crypto\X509.py", line 609, in load_cert
bio = BIO.openfile(file)
File "C:\Python27\lib\site-packages\M2Crypto\BIO.py", line 186, in openfile
return File(open(filename, mode))
IOError: [Errno 13] Permission denied: 'C:/Users/xxx/Documents/temp'
What is wrong in my code please?
You're specifying a folder when you should be specifying a file. According to the documentation for the M2Crypto.X509.load_cert function, you should be specifying the path to a file, not a folder:
Load certificate from file.
#type file: string
#param file: Name of file containing certificate in either DER or PEM format.
If you try and load data from or write data to a folder instead of a file, you will get a "Permission denied" error, at least on Windows. To test this, I created a folder called temp, and tried to read data from it and write data to it, and I got the exact same error as in your question:
Traceback (most recent call last):
File "test.py", line 4, in <module>
with open(r'C:\Users\Random\Documents\temp', 'w') as f:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Random\\Documents\\temp'
Traceback (most recent call last):
File "test.py", line 4, in <module>
with open(r'C:\Users\Random\Documents\temp', 'r') as f:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Random\\Documents\\temp'
In the future, you should look at the documentation for the functions you're using to ensure that you're passing not only the right type of variable to it, but also that the data itself is what the function expects.
Also, in your question you said you're trying to write to a file, but you're using a function which reads from a file. I would suggest going through and making sure you're doing what you think you're doing. Again, reading the documentation for the library you're using will be helpful.
I m using mac and have added rsa key on desktop
The path I 'm using is
host_key = paramiko.RSAKey(filename='~/Desktop/test_rsa.key')
Error:
Traceback (most recent call last):
File "/Users/vidit/PycharmProjects/untitled6/server.py", line 7, in <module>
host_key = paramiko.RSAKey(filename='~/Desktop/test_rsa.key')
File "/Library/Python/2.7/site-packages/paramiko/rsakey.py", line 45, in __init__
self._from_private_key_file(filename, password)
File "/Library/Python/2.7/site-packages/paramiko/rsakey.py", line 163, in _from_private_key_file
data = self._read_private_key_file('RSA', filename, password)
File "/Library/Python/2.7/site-packages/paramiko/pkey.py", line 267, in _read_private_key_file
with open(filename, 'r') as f:
IOError: [Errno 2] No such file or directory: '~/Desktop/test_rsa.key'
You can not use ~ in the path of a file directly. It is a shell feature and expanded by the shell.
Use os.path.expanduser(path) to expand the ~ in the file path before using it.
You can't use ~ in Python paths. Try hardcoding your home directory and it will work.
You can use expanduser() if you want to use tilde (~)
from os.path import expanduser
keypath = expanduser("~/Desktop/test_rsa.key")
I've been working on retrieving a file from my ftp server, the intention is to get the file, untar it on the local machine and compare the MD5 sum to the locally install package. My main focus is getting this file from the ftp server.
After running the script I get the following:
Traceback (most recent call last):
File "./tgzTest.py", line 27, in <module>
proof = tarfile.is_tarfile("test.tgz")
File "/usr/pkg/lib/python2.7/tarfile.py", line 2585, in
is_tarfile t = open(name)
File "/usr/pkg/lib/python2.7/tarfile.py", line 1660, in
open return func(name, "r", fileobj, **kwargs)
File "/usr/pkg/lib/python2.7/tarfile.py", line 1722, in
gzopen fileobj = bltn_open(name, mode + "b")
IOError: [Errno 2] No such file or directory: 'test.tgz'
The following is the code I'm currently using, thanks for any suggestions!
#!/usr/bin/python
import tarfile
import os
import ftplib
from ftplib import FTP
import hashlib
ftpServer = 'myserver.com'
password = 'null'
os.chdir("/home/user/testFolder")
ftp = FTP(ftpServer)
ftp.login('Anonymous', password)
print "You're in"
fileDir = "/pub/pkgsrc/base_pkgs"
tfile = "test.tgz"
ftp.cwd(fileDir)
print ftp.pwd()
tar = tarfile.open("test.tgz", 'r|gz')
for file in tar.getmembers():
print file.name
tar.close()
The tarfile package expects files in the local file system, not on the FTP server. You have to download the file first using the retrbinary() method of the ftp object, and pass the path to the downloaded file to tarfile.open().