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
Related
Can't instantiate objects using python interpreter, please help.
So inside of my python file expresser.py I have something like
class Expresser:
def __init__(self):
pass
...
Now when I type in the terminal
python
and then
>>> import expresser
>>> test_object = Expresser()
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Expresser' is not defined
I'm using PyCharm
when I type where python I get three diff locations so I suspect that but don't know how to rectify
I guess you meant:
from expresser import Expresser
Or:
from expresser import *
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)
I'm trying to run a code:
import os
from os import listdir
for f in sorted(os.listdir("/path")):
if f in f.startswith("20"):
for f in sorted(os.listdir(f)):
if f.endswith(".txt"):
pass
else:
try:
os.system("/path/script.py %s" % f)
except:
pass
I have received this error:
Traceback (most recent call last):
File "files_correct_phase.py", line 5, in <module>
if f in f.startswith("20"):
TypeError: argument of type 'bool' is not iterable
code here
I ran it inside the python prompt and it worked fine after line 5, but when I run it as
python python_script.py
in the command line, it gives me this error. I would be grateful for any advice and/or help.
(Python version 2.7.6)
if f in f.startswith("20"):
is not valid. startswith returns a bool the in keyword trys to check for containment inside your bool. That only works for iterables (which bool is not). You probably want:
if f.startswith("20"):
i'm having a os library error, and it seems that there aren't much info about it on the net. When i try to create a folder in ubuntu 14.04 using my python script:
from os import *
ncpath = "lol"
if not path.exists(ncpath):
makedirs(path,0755)
this error is returned:
File "/home/user/anaconda2/lib/python2.7/posixpath.py", line 85, in split
i = p.rfind('/') + 1
AttributeError: 'module' object has no attribute 'rfind'
can someone help me figure out what's going on ?
Here:
makedirs(path,0755)
You are passing the path module itself instead ncpath which is your string.
I'm having trouble running a compiled app which includes singleton, the .pyw compilation proccess goes just fine, but when I attempt to run the resulting .exe, it writes an error log with the message shown below:
Traceback (most recent call last):
File "main.pyw", line 16, in <module>
File "tendo\singleton.pyc", line 20, in __init__
AttributeError: 'module' object has no attribute '__file__'
this is how I'm calling singleton:
from tendo import singleton
me = singleton.SingleInstance()
The tendo's singleton module makes use of sys.modules['__main__'].__file__ to find the main directory. In py2exe it doesn't exist, that's why you get this error.
You can fix it with this answer. In tendo/singleton.py, line 20 you have:
self.lockfile = os.path.normpath(tempfile.gettempdir() + '/' +
os.path.splitext(os.path.abspath(sys.modules['__main__'].__file__))[0] \
.replace("/","-").replace(":","").replace("\\","-") + '-%s' % flavor_id +'.lock')
Replace with something like this:
path_to_script = get_main_dir() #see linkd answer
self.lockfile = os.path.normpath(tempfile.gettempdir() + '/' + path_to_script
.replace("/","-").replace(":","").replace("\\","-") + '-%s' % flavor_id +'.lock')
Report this as an issue to the author, and/or make a pull request with the fix.