模块

通俗的说模块就把一个已经写好的带有可使用的函数的文件,通过文件名进行导入,然后调用里面的函数等来完成所需功能,模块封装了你需要实现功能的代码,使用者只需调用即可,简化代码量,缩短编程时间。

time模块

实例

        import time
import datatime
print("start to sleep...")      ##time.sleep时间等待5秒
time.sleep(5)
print("wake up ....")
        print(time.time())
#时间戳 从1970年1月1号开始到现在一共过去了多少秒 print(time.ctiem())
#输出当前系统时间 print(time.ctime(time.time()-86640))
#将时间戳转为字符串格式
  print(time.gmtime(time.time()-86640))

  #输出结果time.struct_time(tm_year=2017, tm_mon=8, tm_mday=7, tm_hour=7, tm_min=32, tm_sec=49, tm_wday=0, tm_yday=219, tm_isdst=0) 
            time_obj=time.gmtime(time.time()-86640)
print(time_obj) #根据上面的输出内容进行格式化输出
print(str(time_obj.tm_year) + "-" + str(time_obj.tm_mon) + "-" + str(time_obj.tm_mday))
#结果 2017-8-7 #加上str是因为他们原来是整型的 #用字符串格式化输出
print("%s-%s-%s"%(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday)
#结果为 2017-8-7 格林威治时间
        time.locatime(time.time()-86640)#本地时间(本机时间)

        time.strftime("%Y-%m=%d %H:%M:%S",time.localtime())
#格式化输出时间可以将time.localtime替换为其他时间
#strftime将给定对象转成给定格式 time.strptime("2016/05/22","%Y/%m/%d")
#将 日期字符串 转成 struct时间对象格式
#就是上面那个反过来
#表明时间格式转换成struct时间格式

timedata模块

        print(datetime.date.today())
#输出格式2016-01-26 print(datetime.date.fromtimestamp(timetime()-86400))
#2016-01-26 将时间戳转换为日期格式 current_time = datetime.datetime.now()
print(current_time)
#输出2017-08-08 20:33:12.870346
print(current_time.timetuple)
#返回struct_time格式
print(current_time.replace())#输出现在时间
print(current_time.replace(1996,5,20))#输出给定时间
print(datetime.datetime.now() )#当前时间
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30

sys模块

      import sys
print(sys.argv) #在Terminal中输入python 脚本名.py 加参数
if sys.argv[1] == "" :#假如第一个参数为 120 就输出 gg
print("gg") #否则输出....
else :
print("....") print(sys.path)#输出模块存放目录
        sys.exit(n)
退出程序,正常退出时exit(0)
choice = input("quit?")
if choice == "y" or choice == "Y":
sys.exit("bye") sys.version
获取Python解释程序的版本信息 sys.platform
返回操作系统平台名称 pip.exe
安装模块,详情190 sys.stdout.write("please:")
不换行输出 abc = sys.stdin.read()
#对输入的字符输入到abc中,可多行

sys.stdin.red()

        >>> cmd = sys.stdin.read()
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
>>> print(cmd)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> print(cmd)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >>>
        abc = sys.stdin.readline()
#只能一行
         sys.argv           命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdin 输入相关
sys.stdout 输出相关
sys.stderror 错误相关

OS模块

         os.getcwd()
获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")
改变当前脚本工作目录;相当于shell下cd os.curdir
返回当前目录: ('.') os.pardir
获取当前目录的父目录字符串名:('..') os.makedirs('dirname1/dirname2')
可生成多层递归目录 os.removedirs('dirname1')
若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.mkdir('dirname')
生成单级目录;相当于shell中mkdir(建立一个新的子目录) dirname(目录名) os.rmdir('dirname')
删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir(删除目录) dirname os.listdir('dirname')
列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.remove()
删除一个文件 os.rename("oldname","newname")
重命名文件/目录 os.stat('path/filename')
获取文件/目录信息 os.sep
输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/" os.linesep
输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.pathsep
输出用于分割文件路径的字符串 os.name
输出字符串指示当前使用平台。win->'nt'; Linux->'posix' os.system("bash command")
运行shell命令,直接显示 os.environ
获取系统环境变量 os.path.abspath(path)
返回path规范化的绝对路径 os.path.split(path)
将path分割成目录和文件名二元组返回 os.path.dirname(目录名)(path)
返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path)
返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 os.path.exists(path)
如果path存在,返回True;如果path不存在,返回False os.path.isabs(path)
如果path是绝对路径,返回True os.path.isfile(path)
如果path是一个存在的文件,返回True。否则返回False os.path.isdir(path)
如果path是一个存在的目录,则返回True。否则返回False os.path.join(path1[, path2[, ...]])
将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 os.path.getatime(path)
返回path所指向的文件或者目录的最后存取时间 os.path.getmtime(path)
返回path所指向的文件或者目录的最后修改时间

实例解析

利用模块创建目录文件

        import os

        def fff(path):
if os.path.isdir(path):
print("目录已经存在")
else :
os.mkdir(path)
print("创建成功") a = input("请输入文件名,不能输入特殊字符")
fff(a)

进度条

版本一

import time
import sys
a=0
while a !=100:
for i in range(20):
a += 5
sys.stdout.write("%d%%|"%(a))
for k in range(i):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.5)
sys.stdout.write("\r")#清空本行字符

版本二

    for i in range(21):
sys.stdout.write("\r") #这里的int(i/20*100)*"*"意思是输出多少个"*"
sys.stdout.write("%s%% |%s"%(int(i/20*100),int(i/20*100)*"*"))
sys.stdout.flush()
time.sleep(0.5)

python-time模块、sys模块、os模块以及大量实例的更多相关文章

  1. python中sys和os模块的使用

    在python中,sys,os模块是非常强大的,提供了许多对文件夹.文件和路径的操作方法 sys模块 sys.argv   #命令行执行脚本,其实它就是一个列表 ,sys.argv[0] 是程序自身路 ...

  2. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  3. python学习笔记:sys、os模块

    os模块:负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块:负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. --os 常用方法-- ...

  4. Python模块02/序列化/os模块/sys模块/haslib加密/collections

    Python模块02/序列化/os模块/sys模块/haslib加密/collections 内容大纲 1.序列化 2.os模块 3.sys模块 4.haslib加密 5.collections 1. ...

  5. Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块

    Python第十一天    异常处理  glob模块和shlex模块    打开外部程序和subprocess模块  subprocess类  Pipe管道  operator模块   sorted函 ...

  6. Python的路径操作(os模块与pathlib模块)

    Python的路径操作(os模块与pathlib模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.os.path模块(Python 3.4版本之前推荐使用该模块) #!/u ...

  7. 模块sys,os

    Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库,现在,我们先来象征性的学2个简单的. 在Pyt ...

  8. python模块基础之OS模块

    OS模块简单的来说它是一个Python的系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. 可以查看OS模块的帮助文档: >>> import os #导入os模块 ...

  9. (python) 标准模块sys和os的使用

    一.sys模块 包含了系统的相关的功能.我们来学习sys.argv,它包含命令行参数. 例子:定义了一个add函数,用来实现两个整数的相加. #! coding=utf-8 # usersys.py ...

  10. python中模块sys与os的一些常用方法

    sys模块提供了访问或操作与python解释器相关方法与对象. 我们就列举出常用到的知识,以后,随着学习,不断补充. 几个常用到的动态对象: sys.argv,这是一个列表,它包含了所有传递给脚本的命 ...

随机推荐

  1. maven学习(五)插件和自定义插件

    插件是可以配置在settings.xml和pom.xml中的 插件目标: 在了解插件和生命周期的绑定关系之前,先来说一下插件目标.在实际项目构建的过程中,需要经历编译.打包等等许许多多的操作,为每个操 ...

  2. Template Pattern & Strategy Pattern

    详细见<C++设计模式 23种设计模式.pdf 55页> 在面向对象系统的分析与设计过程中经常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,但是逻辑(算 ...

  3. Docker镜像提交命令commit的工作原理和使用方法

    在本地创建一个容器后,可以依据这个容器创建本地镜像,并可把这个镜像推送到Docker hub中,以便在网络上下载使用. 下面我们来动手实践. docker pull nginx:1.15.3 用命令行 ...

  4. 一种不通过UI给C4C自定义BO创建测试数据的方式

    假设我在Cloud Studio里创建了如下一个非常简单的自定义BO: 我想生成一些该BO的实例.以前我采用的做法是给这个自定义BO创建编辑用的UI.然后使用这些UI创建BO实例.这种方式很花费时间. ...

  5. 在VMware上安装ubuntu——网络类型

    安装虚拟机时,向导提示选择网络类型: 当使用仅主机模式网络时,虚拟机和物理机不能互相访问共享.

  6. vim编辑下几个退出保存的命令

    :w 将数据写入硬盘 :w! 若文件属性为“只读”时,强制写入该文件.不过需要注意,这个是在你的权限可以改变的情况下才能成立 :q 离开vim :q! 修改过文件,又不想保存 :wq 保存后离开 :w ...

  7. bootstrap table 分页只显示分页不显示总页数等数据

    搜了下没找到解决方案,就用CSS来解决了. 把paginationDetailHAlign:"right",使pagination-detail的class为.pull-right ...

  8. [USACO12FEB]牛券Cow Coupons

    嘟嘟嘟 这其实是一道贪心题,而不是dp. 首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的 ...

  9. mysql安装下载

    简单介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最 ...

  10. 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...