Senenium+Python 自学笔记(第七天)

可能会好奇time模块到底在哪里?为什么导入进来就可以用这是pyhon的核心方法,而且经过了编译,所以我们无法看到ctime()函数是如何获取当前系统时间的不过我们可以用通过help来查看time模块的帮助说明

 

time模块命令及详解

 

import time

# 将当前时间转换为时间字符串

print(time.asctime())

# 将指定时间转换时间字符串,时间元组的后面3个元素没有设置

print(time.asctime((2018, 2, 4, 11, 8, 23, 0, 0 ,0))) # Mon Feb 4 11:08:23 2018

# 将以秒数为代表的时间转换为时间字符串

print(time.ctime(30)) # Thu Jan 1 08:00:30 1970

# 将以秒数为代表的时间转换为struct_time对象。

print(time.gmtime(30))

# 将当前时间转换为struct_time对象。

print(time.gmtime())

# 将以秒数为代表的时间转换为代表当前时间的struct_time对象

print(time.localtime(30))

# 将元组格式的时间转换为秒数代表的时间

print(time.mktime((2018, 2, 4, 11, 8, 23, 0, 0 ,0))) # 1517713703.0

# 返回性能计数器的值

print(time.perf_counter())

# 返回当前进程使用CPU的时间

print(time.process_time())

#time.sleep(10)

# 将当前时间转换为指定格式的字符串

print(time.strftime('%Y-%m-%d %H:%M:%S'))

st = '2018年3月20日'

# 将指定时间字符串恢复成struct_time对象。

print(time.strptime(st, '%Y年%m月%d日'))

# 返回从1970年1970年1月1日0点整到现在过了多少秒。

print(time.time())

# 返回本地时区的时间偏移,以秒为单位

print(time.timezone) # 在国内东八区输出-28800

 

 

查看当前路径

import os 导包

os.getcwd() 获取当前路径

os.chdir("E:\\工具") 跳转到制定路径

 

os.mkdir("E:\\工具\\文件夹\world")

在pooject目录中创建两个py文件 test和testing 

在tets中定义方法 

def add(a,b) :

    return a+b

 

在testing中调用test中的方法

from test import add

    print(add(4,5))

 

 

目录或文件操作命令

os.listdir("E:\\工具") 返回指定目录下的所有文件和目录名

os.remove("E:\\工具\\123.txt") 用函数来删除一个文件

os.path.isfile("E:\123\qaz\qwe.txt") 检验给出的路径是否是一个文件

os.path.isdir("E:) 检验给出的路径是否是一个目录

os.path.exists() 检验给出的路径是否真实存在

os.path.split() 返回一个路径和文件名

os.path.splitext("E:\\工具")分离扩展名

os.path.dirname("E:\\工具\\文件夹\\helo\\test.txt") 获取路径

os.path.basename("E:\\工具\\文件夹\\helo\\test.txt") 获取文件名

os.system("E:\\工具\\文件夹\\helo\\test.txt") 运行shell命令

os.makedirs(r“c:\python\test”) 创建多个目录

os.mkond("E:\\工具\\tets") 创建单个目录

os.stat(file) 获取文件属性

os.exit()终止当前进程

os.path.getsize(filename) 获取文件大小

os.mknod("test.txt")创建空文件

fp = open("test.txt",w)   直接打开一个文件

关于open模式

w      以写的方式打开

a       以追加的方式打开(必须创建新文件)

r+     以读写的模式打开

w+    以读写模式打开参见W

a+     以读写模式开始参见A

rb     以二进制模式打开

wb    以二进制写模式打开参见W

ab     以二进制追加模式开大参见A

rb+    以二进制读写模式打开参见r+

wb+   以二进制读写模式打开参见w+

ab+    以二进制读写模式打开参见a+

 

目录

os.mkdir("file") 创建目录

复制文件:

shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件

shutil.copy("oldfile","newfile") oldfile只能是文件夹,newfile可以是文件,也可以是目标目录

复制文件夹:

shutil.copytree("olddir","newdir") olddir和newdir都只能是目录,且newdir必须不存在

重命名文件(目录)

os.rename("oldname","newname") 文件或目录都是使用这条命令

移动文件(目录)

shutil.move("oldpos","newpos")

删除文件

os.remove("file")

删除目录

os.rmdir("dir")只能删除空目录

shutil.rmtree("dir") 空目录、有内容的目录都可以删

转换目录

os.chdir("path") 换路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值