我试图为unix find-command编写一个小包装脚本。东西我搞砸了参数传递。你能给我一个关于我的错误的暗示吗?Python - 被类和实例变量困惑
的错误消息是
Traceback (most recent call last):
File "frep.py", line 43, in <module>
inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)
NameError: name 'self' is not defined
这是代码:
import os
import subprocess
import string
import sys
class Frep(object):
extension = ""
search_string =""
commands = []
rel_numbers = ""
def get_params(self):
Frep.no_of_params = len(sys.argv)
if Frep.no_of_params == 4:
Frep.extension = str(sys.argv[1])
Frep.search_string = str(sys.argv[2])
Frep.rel_numbers = str(sys.argv[3])
else:
print "Usage: frep [FILE_EXTENSION] [SEARCH_STRING] [RELEASE_NUMBERS]"
print "Example: frep sql my_search_string [5-6]"
print " "
sys.exit()
def prep_shell_commands(self, ext, ss, reln):
print ext
tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '["+reln+"]*'", shell=True), '\n')
#tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '[6-7]*'", shell=True), '\n')
for d in tmp_folderlist:
commands.append("find " + d + " -type f -name '*" + ext +"' -exec grep -il '" + ss +"' {} \;")
print commands
def exec_commands(self, c_list):
for c in c_list:
os.system(c)
inst = Frep()
inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)
exec_commands(self.commands)
'self'只存在于以它为参数的方法中。你正试图在其他地方使用它。 – Kevin
所以我如何访问这个实例的类变量? – royskatt
您不访问实例的类变量。您可以访问该类的类变量。有“类变量”和“实例变量”。 “扩展名”是一个类变量,可通过“Frep.extension”进行访问。 – paidhima