02 2021 档案
摘要:OS模块: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd os.curdir 返回当前目录: ('.') os.pardir 获取当前目录的父目录字符串名:('..') os.ma
阅读全文
摘要:1.random.random() #用于生成一个0到1的 随机浮点数:0<= n < 1.0 1 import random 2 a = random.random() 3 print (a) 2.random.uniform(a,b) #用于生成一个指定范围内的随机符点数,两个参数其中一个是上限
阅读全文
摘要:import sys,os# print(__file__) #这个是由paycharm拼接出来的路径,实际值是只有bin.py这个名字# print(os.path.abspath(__file__))#这个拿到的是bin这个执行文件的绝对路径BASE_path=os.path.dirname(
阅读全文
摘要:def readfies(file): #定义读取用户账号和密码的函数,返回一个包含用户名和密码字典的列表 li1=[] with open(file,"r+",encoding="utf-8")as user: users=user.readlines() for i in users: i =
阅读全文
摘要:Python 三种读文件方法read(), readline(), readlines()及去掉换行符\n 首先, 让我们看下数据demo.txt, 就两行数据. 35durant teamGSW 1. read() with open("demo.txt", "r") as f: data = f
阅读全文
摘要:1.排除文件打开方式错误: r只读,r+读写,不创建,即需要事先存在一个文件以供读/读写,若不存在文件会报错 w新建只写,w+新建读写,二者都会将文件内容清零,即事先不需要有该文件存在,若已经存在则会覆盖 (以w方式打开,不能读出。w+可读写) w+与r+区别: r+:可读可写,若文件不存在,报错;
阅读全文
摘要:在使用seek()函数时,有时候会报错为 “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下: >>> f=open("aaa.txt","r+") #以读写的格式打开文件aaa.txt >>> f.read() #
阅读全文