python基础知识(十八)

银行卡解冻

import time
import os
dj = "2019-03-26 16:03:20"
jd = time.mktime(time.strptime(dj,"%Y-%m-%d %H:%M:%S"))
# print("解冻时间点的秒数为:%f秒"%jd)
while True:
    dq = time.time()
    # print("当前时间为:%f秒"%dq)
    sy = jd - dq
    # print("距离解冻还剩:%f秒"%sy)
    a = sy%60
    b = sy//60%60
    c = sy//60//60%24
    d = sy//60//60//24
    os.system("cls")
    print("剩余解冻时长为:%d天,%d小时,%d分,%d秒"%(d,c,b,a))
    time.sleep(1)

结果为:
在这里插入图片描述
文件读取

a = open("D:\\nihaoma\\nihao.txt","r")
x = a.read()
print(x)
a.close()

结果为:
在这里插入图片描述
在这里插入图片描述
读取多行

a = open("D:\\nihaoma\\nihao.txt")
x = a.readline()                #读取一行
while len(x)>0:                 #循环读多行
    print(x)
    x = a.readline()
a.close()

读取所有行

a = open("D:\\nihaoma\\nihao.txt")
x = a.readlines()
print(x)
for line in x:
    print(line)
a.close()

取消换行\n

a = open("D:\\nihaoma\\nihao.txt")
x = a.readlines()
print(x)
for line in x:
    if line[-1]=="\n":
        print(line[:-1])
    else:
        print(line)
a.close()

结果为:
在这里插入图片描述
文件改写

a = open("D:\\nihaoma\\nihao1.txt","w")
b = "你还好吗"
a.write(b)
a.close()

文件内容增加
程序每执行一次,就多一次结果。

a = open("D:\\nihaoma\\nihao2.txt","a")
b = "1234"
a.write(b)
a.close()

指针

a = open("D:\\nihaoma\\nihao2.txt","r")
x = a.read(4)
print(x,a.tell())     #tell告诉我现在的位置长度坐标
y = a.read(5)
print(y)
a.seek(3,0)         #重新设置指针位置,第一个参数是位置的偏移量,
                           #第二个参数是从哪个地方开始,0代表开头,1是当前位置,2是结尾
z=a.read(3)          #截取第三位后面的3个元素
print(z)
a.close()

重命名和删除

#重命名
import os
os.rename("D:\\nihaoma\\nihao3.txt","D:\\nihaoma\\nihao2.txt")
os.rename("D:\\nihaoma\\nihao1.txt","D:\\nihaoma\\nihao4.txt")
#删除
os.remove("D:\\nihaoma\\nihao2.txt")
path = os.getcwd()        #输出当前目录
print(path)
os.mkdir("a\\b\\c",0x777)   #创建文件#124   执行   写    读

b = os.path.exists("D:\\nihaoma\\nihao.txt")    #查找是否存在
print(b)
b = os.path.exists("a\\e")     #判断当前目录的文件是否包含
print(b)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值