Python常用的功能(随机数生成、读写文件、链接数据库)

一、随机数生成

import random 

#生成1-1000之间的伪随机数
num = random.randint(1,1000)  

二、读文件

read = open("c:\\text.txt","r") 
lines = read.readlines() 
for line in lines:  
    print (line) 

三、写文件

w = open("c:\\test.txt","r+") 
w.write("123")

四、连接数据库

import MySQLdb 
conn = MySQLdb.connect (host='localhost',databse='test',user='test',password='123') 
sql= conn.cursor() 
sql.execute("select * from test") 
print (sql.rowcount)
posted @ 2022-11-28 16:46  小小程序员ol  阅读(53)  评论(0编辑  收藏  举报