AttributeError: 'list' object has no attribute 'rfind' - python

This is my code:
with open (basefile,"r") as file:
for servers in file.read().splitlines():
rsltfile = servers+"_SNMPCHECK_RSLT.txt"
command = (["snmp-check","-c"],["community.txt"],servers,[">>"],rsltfile)
with open(rsltfile,"w") as rslt:
subprocess.call(command)
And this is the Traceback for it:
Traceback (most recent call last):
AttributeError: 'list' object has no attribute 'rfind'
I couldn't paste every line in the Traceback because it kept giving me errors. But anyway I can't seem to use the call method and I get this error and I don't know what is happening.

Don't use a nested list/tuple.
>> is a shell feature, which in my tests doesn't seem to work, even with shell = True. It might work if your command was a string instead of a list, but that's not ideal either.
command = ["snmp-check","-c","community.txt",servers]
with open(rsltfile,"a") as rslt: # note the "a"
subprocess.call(command, stdout=rslt)

Related

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

I used indentation method in the code but it throws a error. Need solution please. My python version is Python 2.7.15+
Code:
import textwrap
s = 'hello\n\n \nworld'
s1 = textwrap.indent(text=s, prefix=' ')
print (s1)
print ("\n")
s2 = textwrap.indent(text=s, prefix='+ ', predicate=lambda line: True)
print (s2)
Code is taken from geeksforgeeks
Output Error:
python Textwrap5.py
Traceback (most recent call last):
File "Textwrap5.py", line 4, in <module>
s1 = textwrap.indent(text=s, prefix=' ')
AttributeError: 'module' object has no attribute 'indent'
It might be that your file is located in a directory called textwrap or you have other file called textwrap in the same or parent directory.
Try changing the directory name or the file and see if it works
Python2.7, textwrap doesn't have indent function available.
Either use initial_indent or subsequent_indent as per your requirement OR upgrade to Python3.x

type error: 'module' object is not callable

I just started using python and I get an error when I try to copy an object:
import numpy
import copy
c = numpy.zeros(10)
t = copy(c)
Running the code I encountered this error that I can not solve, could you help me? Thank you all
Traceback (most recent call last):
File "sage_server.py", line 5, in <module>
t = copy(c)
TypeError: 'module' object is not callable
You might be invoking a module as a function (as suggested by the error message).
>>> import copy
>>> type(copy)
<type 'module'>
Instead, what you seem to need is the copy() function, that is included in that module.
>>> type(copy.copy)
<type 'function'>
For that, you would need to do something like:
>>> copy.copy(c)

Capturing nvidia-detect output to list

I want to run nvidia-detect and capture the output in a list or even a string that I can do a string comparison of the output to what I require.
I need to know if the system requires kmod-nvidia or kmod-nvidia-340xx.
I have searched and came up with two possible ways of capturing the output of nvidia-detect.
My initial code was:
test=str(os.system('nvidia-detect'))
print test
my output is:
kmod-nvida
256
where 256 is the value of test.
So after searching I tried:
test2=subprocess.check_output('nvidia-detect', shell=True)
and get this error:
Traceback (most recent call last):
File "/home/emmdom/PycharmProjects/mycode/nvidia_update.py", line 8, in <module>
test2=subprocess.check_output('nvidia-detect')
AttributeError: 'module' object has no attribute 'check_output'
I got it work this is what ended up working for me. Thanks
import os
os.system('yum -y yum-plugin-nvidia nvidia-detect')
nvidia='kmod-nvidia'
nvidia_340='kmod-nvidia-340xx'
chk_nvidia=(os.popen('nvidia-detect').read()).rstrip()
print chk_nvidia
if chk_nvidia == nvidia:
print 'nvidia'
os.system('yum -y kmod-nvidia')
if chk_nvidia == nvidia_340:
print 'nvidia-340-xxx'
os.system('yum -y kmod-nvidia-340xx')

Print Data from PLC to Python using pycomm

I am trying to move data (a dint specifically but my example is a BOOL) from the plc to python to be used as a variable to display a picture. The issue is, if I use pycomm, I am getting an error in Windows Powershell. I feel this is a very simple error from a basic python mistake rather than a pycomm issue but I am not informed enough to tell.
SysInfo:
configparser==3.5.0
cpppo==3.9.7
greenery==2.1
ipaddress==1.0.18
pycomm==1.0.8
pyreadline==2.1
pytz==2017.2
python==2.7.13
Code I am using:
from pycomm.ab_comm.clx import Driver as ClxDriver
import logging
if __name__ == '__main__':
c = ClxDriver()
if c.open('IPADDRESSHERE'):
print(c.read_tag(['new_Bool']))
c.close()
Which is just a stripped down version of one of the examples on github https://github.com/ruscito/pycomm
This is the result from running powershell:
PS C:\Users\Tom\Documents\PythonProjects> python pycomm2.py
Traceback (most recent call last):
File "pycomm2.py", line 10, in
print(c.read_tag(['new_Bool']))
File "C:\Python27\lib\site-packages\pycomm\ab_comm\clx.py", line 359, in read_tag
self.logger.warning(self._status)
AttributeError: 'Driver' object has no attribute 'logger'
PS C:\Users\Tom\Documents\PythonProjects>
I have looked for this AttributeError and tried to find a solution, but I think the solutions I have found are over my head. If I have failed to provide some details in order for this question to make sense please let me know.
Edit:
from pycomm.ab_comm.clx import Driver as ClxDriver
import logging
if __name__ == '__main__':
logging.basicConfig(
filename="ClxDriver.log",
format="%(levelname)-10s %(asctime)s %(message)s",
level=logging.DEBUG
)
c = ClxDriver()
if c.open('IPADRESSHERE'):
print(c.read_tag(['new_Bool']))
c.close()
Yields the same attribute error.
PS C:\Users\Tom\Documents\PythonProjects> python pycommtest.py
Traceback (most recent call last):
File "pycommtest.py", line 15, in
print(c.read_tag(['new_Bool']))
File "C:\Python27\lib\site-packages\pycomm\ab_comm\clx.py", line 359, in read_tag
self.logger.warning(self._status)
AttributeError: 'Driver' object has no attribute 'logger'
PS C:\Users\Tom\Documents\PythonProjects>
I was able to read a value, but not with pycomm. Using CPPPO, I was able to continuously update a variable as needed. This may not answer the question of what was wrong with my old code, but it is my work around in case someone from the future has to do the same thing. Credit to user Liverpool_chris and the abyss of Reddit.
https://www.reddit.com/r/PLC/comments/5x3y5z/python_cpppo_library_writing_to_tag_in_plc/
from cpppo.server.enip.get_attribute import proxy_simple
import time
host = "IPHERE"
while True:
x, = proxy_simple(host).read(( "CPID"))
print x
time.sleep(5)

Python: TypeError while using os.environ.get

I want to access a shell environment variable in my python script.
I am trying this
import os
print os.environ.get["HOME"]
I get this error when I am executing in python
(I am getting the same error in bash also)
Traceback (most recent call last):
File "C:\Users\sraparim\Desktop\GitHub issues\issue #1187\test.py", line 54, in <module>
print os.environ.get["HOME"]
TypeError: 'instancemethod' object has no attribute '__getitem__'
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\sraparim\Desktop\GitHub issues\issue #1187\test.py"]
[dir: C:\Users\sraparim\Desktop\GitHub issues\issue #1187]
[path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\WebEx\Productivity Tools;C:\Program Files (x86)\Cisco\OSD-ShellApplications;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Program Files\PuTTY\;C:\Python27\Scripts;C:\Python27;C:\Python27]
Please help...
get() is a method on the environ object; use parentheses instead of brackets:
print os.environ.get('HOME')
or brackets without get, which implicitly calls __getitem__():
print os.environ['HOME']

Categories