模块

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

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. table是可语义化

    为了使我们的网站更好的被搜索引擎抓取收录,更自然的获得更高的流量,网站标签的语义化就显得尤为重要.所谓标签语义化,就是指标签的含义. 为了更好的理解标签的语义化,先看下面这个例子: <table ...

  2. C# 生成CODE128条码

    using System; using System.Collections.Generic; using System.Data; using System.Drawing;    namespac ...

  3. bzoj 3028 生成函数

    计算完后为 f(x): 根据我翻高数书,终于推倒出来了. (- ̄▽ ̄)-

  4. 使用TimeSpan对象获取时间间隔

    实现效果: 关键知识: TimeSpan对象表是时间间隔或持续时间,两个DateTime对象相减,则会得到一个TimeSpan对象 使用其days ,hours,minutes等属性 实现代码: pr ...

  5. SignalR集成Autofac

    SignalR SignalR集成需要 Autofac.SignalR NuGet 包. SignalR 集成提供SignalR 集线器的依赖集成.由于 SignalR 是内部构件,所以不支持Sign ...

  6. Android学习笔记_44_apk安装、反编译及防治反编译

    一.APK安装 1.首先需要AndroidManifest.xml中加入安装程序权限: <!-- 安装程序权限 --> <uses-permission android:name=& ...

  7. centos安装nodejs二进制包

    1.下载nodejs的二进制包 wget https://nodejs.org/dist/v6.3.1/node-v6.3.1-linux-x64.tar.xz 2.解压下载的安装包 tar xf n ...

  8. Python Json模块中dumps、loads、dump、load函数介绍哦

    来自: https://www.jb51.net/article/139498.htm 1.json.dumps()       json.dumps()用于将dict类型的数据转成str,因为如果直 ...

  9. oracle中lock和latch的用途

    本文向各位阐述Oracle的Latch机制,Latch,用金山词霸翻译是门插栓,闭锁,专业术语叫锁存器,我开始接触时就不大明白为什么不写Lock,不都是锁吗?只是翻译不同而以?研究过后才知道两者有很大 ...

  10. 对UIImageView+WebCache的封装

    UIImageView+SDWebImage.h #import <UIKit/UIKit.h> typedef void(^DownloadImageSuccessBlock)(UIIm ...