|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
import os
import time
import easygui as g
import sys
import pickle
def fx(path): #检测目录正确路径
if os.path.isdir(path):
pass
else:
os.makedirs(path)
print("创建文件夹"+path)
def fx1(lis1,lis2,n): #定义方法实现随机抽取
for e in range(1,n+1):
x=random.randint(0,len(lis1)-1)
lis2.append(str(e)+'. '+lis1[x])
del lis1[x]
def fx2(list1,list2,list3):
list3.append('日期:\t\t\t\t姓名:\t\t\t\t分数:\n\n')
list3.append('一、填空题(共15题,每题4分)\n')
list0[1](list1,list3,15)
list3.append('\n二、简答题(共4题,每题10分)\n')
list0[1](list2,list3,4)
def fx3(str1,str2,list3):
TK1 = open(path1+str1) #读取
TK2 = open(path1+str2)
list1= TK1.readlines() #获取TK(题库)内容到lis1
list2= TK2.readlines()
list3.append(name+'\n')
list0[2](list1,list2,list3)
ST = open(path2+name+'.txt', 'w') #实现创建
ST.writelines (list3) #写入lis2到ST(试题)
TK1.close() #以下关闭文件
TK2.close()
ST.close()
sj=time.strftime("\n%Y/%m/%d-%H:%M:%S") #生成日志
RZ=open(path3+"rizhi.txt","a")
RZ.write(sj+"\n")
RZ.writelines (list3)
RZ.close()
def ed():
a=g.msgbox('程序即将退出\n导致退出的原因可能有以下几点:\n1.你中途退出了程序\n2.输入了不合法的字符\n',title,image='jw1.gif' ,ok_button='确定')
sys.exit(0)
str1="\\rtk1.txt"
str2="\\rtk2.txt"
str3="\\htk1.txt"
str4="\\htk2.txt"
path1 = os.getcwd()+"\\Bank\\" #定义路径变量
path2 = os.getcwd()+"\\试卷\\"
path3 = os.getcwd()+"\\日志\\"
list0=[fx,fx1,fx2,fx3,ed]
pkf1=open(path3+'myf.pkl','wb')
pickle.dump(list0,pkf1)
pkf1.close()
pkf2=open(path3+'myf.pkl','rb')
mylist=pickle.load(pkf2)
pkf2.close()
print(mylist)
===================== RESTART: D:\python_example\pickle2.py ====================
[<function fx at 0x0000012EC336D0D0>, <function fx1 at 0x0000012EC336D1F0>, <function fx2 at 0x0000012EC33C01F0>, <function fx3 at 0x0000012EC33C0280>, <function ed at 0x0000012EC33C0310>]
运行正常
但是我新建一个.py文件后就报错
import random
import os
import time
import easygui as g
import sys
import pickle
path3 = os.getcwd()+"\\日志\\"
pkf2=open(path3+'myf.pkl','rb')
mylist=pickle.load(pkf2)
pkf2.close()
print(mylist)
===================== RESTART: D:\python_example\pickle1.py ====================
Traceback (most recent call last):
File "D:\python_example\pickle1.py", line 9, in <module>
mylist=pickle.load(pkf2)
AttributeError: Can't get attribute 'fx' on <module '__main__' (built-in)>
有没有大佬解释一下什么原因 我是初学不太懂,还不知道错在哪里,不是说好的什么都可以往里边装吗? |
|