一、多文件编程
cal.py
def add(x,y):
z = x + y
return z
def max(x,y):
if(x > y):
return x
else:
return y
main.py
from cal import add
if __name__ == "__main__":
a = 0
b = 0
a = max(10, 20)
print("max",a)
else:
print("从当前文件执行的")
dir() 可以找到模块内定义的所有名称
9、try/except, try/except/else try-finally 语句无论是否发生异常都将执行最后的代码
try:
f = open("hello.txt",'w')
f.write("hello")
except IOError:
print("打开文件错误\n")
else:
print("打开文件正常\n")
f.close()
10、文件
import os
def creat_verilog_file(filename):
#打开文件
print("打开文件")
testfile = open(filename,'w')
#操作
print("操作")
testfile.write("module text(input clk, input rst);\n")
testfile.write("always@(posedge clk)begin\n\n")
testfile.write("end\n")
testfile.write("endmodule\n")
print("操作完成")
#关闭
print("关闭文件")
testfile.close()
return
def creat_testbench_file(filename):
#打开文件
print("打开文件")
testfile = open(filename,'w')
#操作
print("操作")
testfile.write("module text(input clk, input rst);\n")
testfile.write("always@(posedge clk)begin\n\n")
testfile.write("end\n")
testfile.write("endmodule\n")
print("操作完成")
#关闭
print("关闭文件")
testfile.close()
return
os.mkdir("verilog_project")
os.chdir("verilog_project")
for i in range(1,101):
creat_verilog_file("verilog"+str(i)+".v")
creat_testbench_file("testbench"+str(i)+"tb.v")