python基础(二)多文件编程、文件操作

一、多文件编程
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")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值