python-time模块、sys模块、os模块以及大量实例
模块
通俗的说模块就把一个已经写好的带有可使用的函数的文件,通过文件名进行导入,然后调用里面的函数等来完成所需功能,模块封装了你需要实现功能的代码,使用者只需调用即可,简化代码量,缩短编程时间。
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模块以及大量实例的更多相关文章
- python中sys和os模块的使用
在python中,sys,os模块是非常强大的,提供了许多对文件夹.文件和路径的操作方法 sys模块 sys.argv #命令行执行脚本,其实它就是一个列表 ,sys.argv[0] 是程序自身路 ...
- 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 ...
- python学习笔记:sys、os模块
os模块:负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块:负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. --os 常用方法-- ...
- Python模块02/序列化/os模块/sys模块/haslib加密/collections
Python模块02/序列化/os模块/sys模块/haslib加密/collections 内容大纲 1.序列化 2.os模块 3.sys模块 4.haslib加密 5.collections 1. ...
- Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块
Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函 ...
- Python的路径操作(os模块与pathlib模块)
Python的路径操作(os模块与pathlib模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.os.path模块(Python 3.4版本之前推荐使用该模块) #!/u ...
- 模块sys,os
Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库,现在,我们先来象征性的学2个简单的. 在Pyt ...
- python模块基础之OS模块
OS模块简单的来说它是一个Python的系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. 可以查看OS模块的帮助文档: >>> import os #导入os模块 ...
- (python) 标准模块sys和os的使用
一.sys模块 包含了系统的相关的功能.我们来学习sys.argv,它包含命令行参数. 例子:定义了一个add函数,用来实现两个整数的相加. #! coding=utf-8 # usersys.py ...
- python中模块sys与os的一些常用方法
sys模块提供了访问或操作与python解释器相关方法与对象. 我们就列举出常用到的知识,以后,随着学习,不断补充. 几个常用到的动态对象: sys.argv,这是一个列表,它包含了所有传递给脚本的命 ...
随机推荐
- GitHub无法push的问题
问题背景 换了台别人用过的电脑想要将文件push到github上,出现下面报错 remote: Permission to *****(我的)/gittest.git denied to *****( ...
- Excel数据导入Sql Server,部分数字为Null
在Excel中,我们时常会碰到这样的字段(最常见的就是电话号码),即有纯数字的(如没有带区号的电话号码),又有数字和其它字符混合 (如“区号-电 话号码”)的数据,在导入SQLServer过程中,会发 ...
- 3.GlusterFS 企业分布式存储的搭建
3.1 硬件要求 一般选择 2U 机型,磁盘 SATA 盘 4TB,如果 IO 要求比较高,可以采购 SSD 固态硬盘.为了充分保证系统的稳定性和性能,要求所有 glusterfs 服务器硬件配置尽量 ...
- LeetCode Two Sum 解题思路(python)
问题描述 给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值. 您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次. 方法 1: 蛮力 蛮力方法很简单.循环遍历每个元素 ...
- dos基础+环境搭建基础理论
dos基础 市面上两大操作系统 windows.*nix(unix.linux.mac.bsd(安全性比较高)) 后三种都属于unix的衍生版本 linux是为了兼容unix开发的,最后开放了源代码 ...
- cs229 斯坦福机器学习笔记(一)-- 入门与LR模型
版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/Dinosoft/article/details/34960693 前言 说到机器学习,非常多人推荐的学习资 ...
- 大整数乘法(POJ2389)
题目链接:http://poj.org/problem?id=2389 #include <stdio.h> #include <string.h> #define Max 1 ...
- 深度优先搜索(dfs),城堡问题
题目链接:http://poj.org/problem?id=1164 1.深搜,每个点都访问一次,没有标记的话,就做深搜,同时标记. #include <iostream> #inclu ...
- 【PHP后台】接入支付宝
我使用PHP主要是为客户端做后台使用,并不会做前端网页. 这两天因为公司项目需要,必须接入支付功能,而支付宝当然首当其冲,考虑迭代版本的需要,首先接入支付宝功能,其他的支付功能以后迭代版本的时候 ...
- Hive UDF 用户自定义函数 编程及使用
首先创建工程编写UDF 代码,示例如下: 1. 新建Maven项目 udf 本机Hadoop版本为2.7.7, Hive版本为1.2.2,所以选择对应版本的jar ,其它版本也不影响编译. 2. po ...