I'm trying to write a script which detects whether the machine that the script is run on is a virtual machine or a physical machine and I don't understand the error and how to fix it.
import wmi
def sys_info():
objWMIService = wmi.GetObject("winmgmts:\root\cimv2")
colItems = objWMIService.ExecQuery("Select * from Win32_BaseBoard")
for objItem in colItems:
print "inside"
Manufacturer = objItem.Manufacturer
if Manufacturer == "Microsoft Corporation":
print "Virtual Machine"
else:
print "Not in one"
The error:
Traceback (most recent call last):
File "C:\Documents and Settings\xxx\Desktop\Python\Practice Code\System information\trial.py", line 16, in <module>
sys_info()
File "C:\Documents and Settings\xxx\Desktop\Python\Practice Code\System information\trial.py", line 5, in sys_info
objWMIService = wmi.GetObject("winmgmts:""\root\cimv2")
File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 72, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 87, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147217375, 'OLE error 0x80041021', None, None)
I'm hoping someone can help, I'm pretty new to python. Thanks.
Do things improve if you change:
objWMIService = wmi.GetObject("winmgmts:\root\cimv2")
to
objWMIService = wmi.GetObject(r"winmgmts:\root\cimv2")
The '\r' sequence in '\root' will be interpreted as a <CR> character in your code. You either have to doubleup the '\'s to escape them so they will be treated as backslashes, or precede the first double quote with 'r' (as I have done), to indicate to Python that this should be a "raw" string literal. Raw strings are no different from regular strings, but the raw string syntax tells the Python compiler to not interpret backslashes.
Related
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.
doc_holder_str = ''
sample_H_value = open("C:\testGupixwin\BX-N-H.HED", "r")
standard_conc_value = open("C:\testGupixwin\gupixwin_H_stdConc.txt", "r")
sample_H_value_str = sample_H_value.readline()
while sample_H_value_str is not '' :
stripper_sample_H = float(sample_H_value_str[5:].lstrip(' '))
I'm trying to write a piece of code (as shown above) which reads some values, do some calculations on it and returns the said values. I am using the LiClipse IDE, for python.
I have tested my code and it works, but when I tried to run it with real data, ( for which I created a new folder to put in all the files I will be working with) I received an OS error suggesting I inserted an invalid argument.
The error report says ;
Traceback (most recent call last):
File "C:\Python34\workspace\Gupixwin_Project.py", line 11, in <module>
sample_H_value = open("C:\testGupixwin\BX-N-H.HED", "r")
OSError: [Errno 22] Invalid argument: 'C:\testGupixwin\\BX-N-H.HED'
on clicking on the C:\testGupixwin\\BX-N-H.HED it bring up a message box suggesting, and I quote,
The definition was found at C:\testGupixwin\BX-N-H.HED, (which
cannot be opened because it is a compiled extension)
I must point out that I feel the error is that the system sees ...\\BX-N.... Instead of ..\BX-N... Which I expect.
Some one suggested I do this
[Open Window -> Preferences, goto PyDev -> Editor -> Code Style ->
File Types, look for "Valid source files (comma-separated)" and append
", log".]
I HAVE DONE THIS BUT I STILL GET THE OSERROR REPORT.
Thanks for any assistance
I think the problem is the escaping with \
alternate the string in: open("C:\testGupixwin\BX-N-H.HED", "r")
with:
open( r"C:\testGupixwin\BX-N-H.HED", "r" ) #rawstring
# or
open( "C:\\testGupixwin\\BX-N-H.HED", "r" ) #escaping the '\' with '\'
(also do this in the following line)
When I try to run my code and import an array from another file I keep getting a Syntax Error saying:
Traceback (most recent call last):
File "run.py", line 21, in <module>
passwords = ast.literal_eval(open("passwords.txt").readlines()[0])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
[‘test’]
^
SyntaxError: invalid syntax
The piece ['test'] is the contents of the external file "passwords.txt", however what confuses me is that in the following code if I include only the first 2 variables the code runs perfectly fine but when the second is added it throws an error (also the file contents is the same in all of the variables)
adjectives = ast.literal_eval(open("Adjectives.txt").readlines()[0])
nouns = ast.literal_eval(open("Nouns.txt").readlines()[0])
passwords = ast.literal_eval(open("passwords.txt").readlines()[0])
usernames = ast.literal_eval(open("usernames.txt").readlines()[0])
The only other related new code is this:
def WOPR():
login = raw_input("Username: ")
login_password = raw_input("Password: ")
login_password = psudo_encrypt(login_password)
for name in usernames:
if name == login:
for word in passwords:
if word == login_password:
print("Greetings " + login)
play(true, login)
else:
print("Incorrect Password \n-------Connection Terminated-------")
elif login == "Joshua":
print("Greetings Professor Falken \nWould you like to play a game?")
play(true, "Professor")
else:
WOPR()
Anyone any idea what could be causing this? Have I missed a really obvious syntax issue? Thanks in advance
[‘test’]
^ ^
The two characters I've marked are curly apostrophes (‘’), not straight apostrophes ('). You probably got them by copying and pasting code from a web site, or by editing your code in a text editor that isn't intended for working with code.
Replace those characters with normal apostrophes.
Hi I tried to generate the genesis file but get this error:
C:\Python34>python mk_genesis_block.py --extradata 0x11bbe8db4e347b4e8c937c1c837
0e4b5ed33adb3db69cbdb7a38e1e50b1b82fa > genesis_block.json
File "mk_genesis_block.py", line 293
print json.dumps(evaluate(), indent=4)
^
SyntaxError: invalid syntax
Edit:
Here is the surrounding lines:
if __name__ == '__main__':
print json.dumps(evaluate(), indent=4)
Then it's EOF. The whole file can be viewed here
Since the offending line seems to be only output, I commented it and got another error:
C:\Python34>python -tt mk_genesis_block.py --extradata 0x11bbe8db4e347b4e8c937c1
c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa > genesis_block.json
Traceback (most recent call last):
File "mk_genesis_block.py", line 124, in <module>
EXTRADATA = (d[2:] if d[:2] == '0x' else d).decode('hex')
AttributeError: 'str' object has no attribute 'decode'
which in conjunction with the other error makes me wonder whether a string instead of a json object is being operated on? Here is the whole arg parsing part:
# Grab the extra data command line argument
if '--extradata' in sys.argv:
d = (sys.argv+[None])[sys.argv.index('--extradata') + 1]
EXTRADATA = (d[2:] if d[:2] == '0x' else d).decode('hex')
else:
EXTRADATA = ''
I also made a test file importing the json package, dumps and decode methods work.
print in python3 is a method not a statement print( "text" ) ... also I believe str.decode was remove in python3 ... instead use codecs.decode(my_str,encoding)
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)