python sqlite3 connection path with spaces - python

I am trying to connect with a full path but I get this problem
>>> path = "/home/astro/Fun LAB/DBlist"
>>> db = sql.connect(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: unable to open database file
>>>
i 've also tried this
>>> path = "/home/astro/Fun\ LAB/DBlist"
>>> db = sql.connect(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: unable to open database file
>>>
I know it's because of the whitespace cause I've tried this and it works
>>> path = "/home/astro/DBlist"
>>> db = sql.connect(path)
>>>
so is there is an easy way to escape the whitespaces in the path or am I doing something wrong?

You might try the uri style approach - it worked for me in getting past connection issues due to spaces in file paths with sqlite3:
Escaping the spaces with %20
prefixing the path with file:///
Examples:
file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
file:///C:/Documents%20and%20Settings/fred/Desktop/

I would suggest naming files or folders without spaces. (I don't think it's possible either) Instead, use dash or underscores.
The next problem is to see where you're running your Python interpreter. If it's nested inside a directory and you're trying to create a path from the outside, then your link would raise an error.
>>> path = "/home/astro/Fun LAB/DBlist"
or
>>> path = "/home/astro/Fun\ LAB/DBlist"
or
>>> path = "/home/astro/DBlist"
wouldn't work if you run python inside these directories or from another.
To correct this, try:
>>> path = "../home/path/to/file"

Having spaces is indeed a problem.
I would suggest bring a symlink and work with that:
!ln -sf '$path_to_db_file' .
db = sql.connect('db_file')

Related

issues running a face_recogition file using editors and cmd

When I run the code below in jupyter, it is excellent, I get the results I want.
real = fr.load_image_file("image.jpg")
unknown = fr.load_image_file("image2.jpg")
# encodings
real_encoding = fr.face_encodings(real)[0]
unk = fr.face_encodings(unknown)[0]
result = fr.compare_faces([real_encoding], unk)
print(result)
but when I copy and paste the same code to an editor(atom the one I am using), and then run it using command prompt, I get an error as shown below
Traceback (most recent call last):
File "face_recognizer.py", line 13, in <module>
real_encoding = fr.face_encodings(real)[0]
IndexError: list index out of range
I activated the environment in which I was running the cmd.
Can someone advise me where I maybe going wrong?
There was no face captured in the photo.
That is why it throws an exception.

'No such file or directory' ERROR information when using "PRINT"

When I 'print' some hex string, some interesting error information in python, I wonder why this error is caused.
Win10(I tried it on ubuntu, No ERROR),python 2.7
enc_hex = '''f982f01c'''
enc_ascii = enc_hex.decode('hex')
print(enc_ascii)
Traceback (most recent call last):
File ".\xxxx.py", line 7, in <module>
print(enc_ascii)
IOError: [Errno 2] No such file or directory
Well,in fact I want to know why "print" a special set of hex will cause file operation, other hex string will not error
Try using codecs.decode:
import codecs
enc_hex = '''f982f01c'''
enc_ascii = codecs.decode(enc_hex, 'hex')
print(enc_ascii)
Output:
b'\xf9\x82\xf0\x1c'
it seems like directory problems . in windows you have to use forward slash (/) while accessing the directory.similar was happened in my case then i use forward slash in windows then it works.

AttributeError: 'module' object has no attribute 'Describe'

I am very new to Python. Reading up on new material for class, I had to copy and paste a few examples to see how things work. So, I copied and pasted code from this website ( https://www.e-education.psu.edu/geog485/node/54 ) under the topic 'Looping in GIS Models'.
Here's mine:
import arcpy
try:
arcpy.env.workspace = "C:\Users\dan and kathryn\Desktop\school\programming\Lesson1"
fcList = arcpy.ListFeatureClasses()
for featureClass in fcList:
arcpy.CopyFeatures_management (featureClass, "C:\Users\dan and kathryn\Desktop\school\programming\Lesson 2\PracticeData/" + featureClass)
except:
print "Script failed to complete"
print arcpy.GetMessages(2)
Here's the error msg:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonWin\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\dan and kathryn\Desktop\loops_gis.py", line 1, in <module>
import arcpy
File "C:\Users\dan and kathryn\Desktop\arcpy.py", line 5, in <module>
desc = arcpy.Describe(featureClass)
AttributeError: 'module' object has no attribute 'Describe'
Contacted my teacher and he wrote code and sent it to me but everything matches. All of the backslashes, indentations, everything except for folder locations. Not sure what is going on but he suspects this problem runs beyond what I've written in my code.
I've already had to alter the site-packages folder within pythonWin and it now contains:
# .pth file for the PyWin32 extensions
win32
win32\lib
Pythonwin
C:\Program Files (x86)\ArcGIS\Desktop10.1\bin
C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.1\ArcToolbox\Scripts
Not sure what's going on or why 'Describe' is even popping up in the problems
Whenever writing path string, use r"path with backslash". It will take care of the backslash and forward slash problem. Use 'os' module when, the paths needed to be joined.
heres what i had the change (all in the backslashes, I had no idea):
arcpy.env.workspace = "C:/Users/dan and kathryn/Desktop/school/programming/Lesson1"
AND
arcpy.CopyFeatures_management (featureClass, "C:\Users\dan and kathryn\Desktop\school\programming\Lesson 2\PracticeData/" + featureClass)

Python os.isfile assert failing

So I'm having problems opening a file for reading, and so I decided to just try an os.isfile assert:
from Android_API_Parser import Android_API_Parser
import os.path
assert os.path.isfile("D:\Work\Python Workspace\Android_API_Parser\test.txt")
tester = Android_API_Parser()
tester.setFile("test.txt")
tester.parse()
It's failing the assert:
Traceback (most recent call last):
File "D:\Work\Python Workspace\Android_API_Parser\src\Android_API_Tester.py", line
9, in <module>
assert os.path.isfile("D:\Work\Python Workspace\Android_API_Parser\test.txt")
AssertionError
I have opened the path to the file I'm trying to open and pasted it below:
D:\Work\Python Workspace\Android_API_Parser\test.txt
Any ideas as to why it's even failing the assert? Unless I'm really tired, the file is clearly located there. I even tried both with "/" and "\", even with escape characters included.
In a string literal, you must escape backslashes with another backslash, use a raw string, or use forward slashes. Otherwise, "\t" becomes a string that just contains the tab character.
Try any of:
assert os.path.isfile("D:\\Work\\Python Workspace\\Android_API_Parser\\test.txt")
assert os.path.isfile(r"D:\Work\Python Workspace\Android_API_Parser\test.txt")
assert os.path.isfile("D:/Work/Python Workspace/Android_API_Parser/test.txt")
assert os.path.isfile(os.path.join("D:", "Work", "Python Workspace",
"Android_API_Parser", "test.txt"))
The file may also not be a regular file. Use os.path.exists to see if it exists.
You may also have insufficient privileges to see the file, or the file name you expect may be localized. To debug this, run:
path = ["Work", "Python Workspace", "Android_API_Parser", "test.txt"]
f = 'D:'
for p in path:
f = os.path.join(f, p)
print(f)
assert os.path.exists(f)
assert os.path.isfile(f)

How to interact with pynessus

I am using http://code.google.com/p/pynessus/ so that I can interact with nessus using python but I run into problems trying to connect to the server. I am not sure what I need to set pynessus too?
I try connecting to the server using the following syntax as directed by the documentation on the site but I receive the following error:
n = pynessus.NessusServer(localhost, 8834, root, password123)
Error:
root#bt:~/Desktop# ./nessus.py
Traceback (most recent call last):
File "./nessus.py", line 634, in
n = pynessus.NessusServer(localhost, 8834, root, password123)
NameError: name 'pynessus' is not defined
The problem is that you didn't import the pynessus module. To solve this problem, simply place the downloaded pynessus.py in the same folder as your Python script and add the line
import pynessus
at the top of that script. You can reference the pynessus library in your script only after that line.

Categories

Resources