我剪切了我试图完成的脚本的一部分。我希望在每次迭代后where categories.CategoryID=1;
得到一个新结果。 from django.contrib.auth.management import create_permissions
from django.contrib.auth.models import Permission
from django.apps import apps
def fix_user_permission():
"""
run this method via shell whenever any amendments in any of the tables is made
"""
print "fixing user permissions"
# delete pre-existing user permission
Permission.objects.all().delete()
apps.models_module = True
create_permissions(apps, verbosity=0)
apps.models_module = None
print "process completed - fixed user permissions"
和mylineS.split()[0]
是每次迭代outS.txt
的结果,每次迭代的结果都不同。但是outT.txt
为每次迭代带来了第一个结果。
我觉得我的做法有问题,有什么想法吗?
commandC
commandC是mylineS.split()[0]
之上的一行。我写的是底部。
B = 0
while B < len(Source_Tdevs):
devS = Source_Tdevs[B]
devT = Target_Tdevs[B]
subprocess.run(commandC, shell=True)
print (devS)
with open('outS.txt', 'r') as gS:
CS = len(gS.readlines())
mylineS = linecache.getline('outS.txt', CS -1)
Source_Tdevs_SGs.append(mylineS.split()[0])
**print (mylineS.split()[0])**
gS.close()
with open('outT.txt', 'r') as gT:
CT = len(gT.readlines())
mylineT = linecache.getline('outT.txt', CT - 1)
Target_Tdevs_SGs.append(mylineT.split()[0])
gT.close()
subprocess.run('del outS.txt, outT.txt', shell=True)
B= B + 1
答案 0 :(得分:0)
您滥用 linecache
模块。 linecache用于从Python源代码中获取源代码行:
linecache模块允许用户从Python源文件中获取任何行,同时尝试使用缓存进行内部优化,这是从单个文件中读取许多行的常见情况。跟踪模块使用它来检索包含在格式化回溯中的源代码行。
正如文本所暗示的那样,模块也会将文件内容保留在内存中,因此只在第一次运行时才提供正确的输出。一个简单的补救措施是使用
使缓存无效.readlines()
虽然更好的做法是不根本不使用linecache(它不是为了这个,而且你的文件毕竟不断变化);而只是阅读使用[-1]
的所有行,并使用lines = gS.readlines()
last_line = lines[-1]
提取最后一行,例如:
subset