#跟着教程学 11、 os模块和datetime模块

os :opreate system

OS模块是Python标准库中的,一个用于访问操作系统功能的模块

通用操作:1、获取平台信息。2、对目录的操作。3、判断操作。

1、系统操作。

os.sep:主要用于系统路径的分隔符。Windows系统是‘\‘。

os.name:指示正使用的工作平台。比如Windows是‘nt’。

os.getenv(环境变量名称):读取环境变量

os.getcwd():获取当前路径(软件路径)

import os

print (os.sep)
print (os.name)
print (os.getenv(‘path‘))
print (os.getcwd())

data_dir = os.sep.join([‘hello‘, ‘world!‘])
print data_dir

-----打印分割线--------

\
nt
C:/Program Files/Autodesk/Maya2018/plug-ins/ATF/ATF;C:/Program Files/Autodesk/Maya2018/plug-ins/ATF/ATF/alias;C:/Program Files/Autodesk/Maya2018/bin/Cg;C:/Program Files/Autodesk/Maya2018/bin;C:/Program Files (x86)/Common Files/Intel/Shared Libraries/redist/intel64_win/compiler;C:/Program Files/Thinkbox/Deadline/bin;C:/ProgramData/Oracle/Java/javapath;C:/Windows/system32;C:/Windows;C:/Windows/System32/Wbem;C:/Windows/System32/WindowsPowerShell/v1.0/;C:/Program Files (x86)/NVIDIA Corporation/PhysX/Common;C:/Program Files (x86)/Windows7Master;C:/Program Files (x86)/QuickTime/QTSystem;;C:/Program Files/Common Files/Autodesk Shared/;C:/Program Files (x86)/Autodesk/Backburner/;C:/Program Files/NVIDIA Corporation/NVIDIA NvDLISR;C:/ProgramData/Redshift/bin;C:/Program Files/Autodesk/Bifrost/Maya2018/bin;C:/Program Files/Autodesk/Maya2018/plug-ins/MASH/bin;d:/solidangle/mtoadeploy/2018/bin;C:/Yeti-v2.2.5_Maya2018-windows64/bin;C:/Program Files/Autodesk/Maya2018/plug-ins/substance/bin;C:/Program Files/Autodesk/Maya2018/plug-ins/xgen/bin
C:\Program Files\Autodesk\Maya2018\bin
hello\world!

2、目录操作-增删改查

os.listdir():返回指定目录下的所有文件和目录名。

os.mkdir():创建一个目录。只创建一个目录文件。

os.rmdir():删除一个空目录。如果目录中有文件则无法删除。

os.makedirs(dirname):可以生成多层递归目录。如果目录全部存在,则创建失败。

os.removedirs(dirname):可以删除多层递归的空目录。若目录中有文件则无法删除。

os.chdir():改变当前目录,到指定目录中。

os.rename():重命名目录名或者文件名。

os.open(file,flag[,model]):打开一个文件,并设置需要打开的选项。(这里可以创建一个文本,或者.py等等文件) 具体可以看这里https://www.runoob.com/python/os-open.html

import os
#输出当前的目录下的所有文件和文件夹
dirs = "D:\Documents\maya\projects"
files = os.listdir(dirs)
print files

#创建一个名为ABC的文件夹
#创建一个名为hello的文件夹,里面还包含hi文件夹

os.mkdir(‘D:\Documents\maya\projects\ABC‘)
os.makedirs(‘D:\Documents\maya\projects\hello\hi‘) 

#删除ABC文件夹
#删除hello\hi文件夹
os.rmdir(‘D:\Documents\maya\projects\ABC‘)
os.removedirs(‘D:\Documents\maya\projects\hello\hi‘)

#创建一个ABC文件夹,并在里面创建名为test.txt的文档(特别注意这里的斜杠是‘/’)。
#os.O_RDWR : 以读写的方式打开。os.O_CREAT: 创建并打开一个新文件
os.mkdir(‘D:\Documents\maya\projects\ABC‘)
fd = os.open(‘D:/Documents/maya/projects/ABC/test.txt‘, os.O_RDWR|os.O_CREAT)

#文档里写入‘hello,world!’并关闭文件。
#注意一定要关闭文件!!要不然文件在后台会一直处于打开状态,不能删除。
os.write(fd,‘hello,world!‘)
os.close(fd)

#重命名文档
os.rename(‘D:/Documents/maya/projects/ABC/test.txt‘,‘D:/Documents/maya/projects/ABC/Goodbye.txt‘)

#删除Goodbye文档
os.remove(‘D:/Documents/maya/projects/ABC/Goodbye.txt‘)

3、判断

os.path.exists(path):判断文件或者目录是否存在。存在则返回True,否则返回False。

os.path.isfile(path):判断是否为文件。是则返回True,否则返回False。

os.path.isdir(path):判断是否为目录。是则返回True,否则返回False。

4、path模块

os.path.basename(path):返回文件名

os.path.dirname(path):返回文件路径

os.path.getsize(name):获得文件大小,如果name是目录返回0L

os.path.abspath(name):获得绝对路径

os.path.join(path,name):链接目录与文件名或目录

os.path.split(path):切割文件名和路径

https://www.runoob.com/python/python-os-path.html

原文地址:https://www.cnblogs.com/3lina/p/11883789.html

时间: 2024-08-30 12:34:00

#跟着教程学 11、 os模块和datetime模块的相关文章

Python中的time模块与datetime模块用法总结

http://www.jb51.net/article/87721.htm time模块time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. time用struct_time表示时间 ? 1 2 3 4 5 6 7 8 import time # time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24,           tm_hour=14, tm_min=17, tm_sec=26,          

(转)python time模块和datetime模块详解

python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共有九个元素组. c.format time 格式化时间,已格式化的结构使时间更具可读性.包括自定义格式和固定格式. 1.时间格式转换图: 2.主要ti

Python常用模块——time&datetime模块

Python常用模块--time&datetime模块 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不做介绍). 我们写程序时对时间的处理可以归为以下3种: 1.时间的显示:在屏幕显示,记录日志等. 2.时间的转换:比如把字符串格式的日期转换成Python中的日期类型. 3.时间的运算:计算两个日期间的差值等. 一.time模块 在Python中,通常有以下3种方式来表示时间: 1.时间戳(time

time模块和datetime模块

http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共有九个元素组. c.format time 格式化时间,已格式化的结构使时间更具可读性.包括自定义格式和固定格式. 1.时间格式转换图: 2.主要time生成方法和time格式转换方法实例: import ti

python time模块和datetime模块详解

一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共有九个元素组. c.format time 格式化时间,已格式化的结构使时间更具可读性.包括自定义格式和固定格式. 1.时间格式转换图: 2.主要time生成方法和time格式转换方法实例: #! /usr/bin/env python # -*- coding:utf-8 -*- # __auth

Python中time模块和datetime模块的常用操作以及几种常用时间格式间的转换

最常见以及常用的几种时间格式 1.时间戳(timestamp),时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 2.时间元组(struct_time),共有九个元素组. 3.格式化时间(format time ),可以格式化为固定或者自定义格式,增加可读性. #!/usr/bin/env python # -*- coding:utf-8 -*- import time #时间戳格式,默认获取当前时间 1500029143.7640195 timestamp = tim

6、模块详解之二 time模块,datetime模块

1.time模块 time.time() #返回时间戳 print(time.time()) 1465609581.970584 time.ctime(seconds) #返回指定时间戳的系统日期 print(time.ctime()) Sat Jun 11 09:47:58 2016 #参数:时间戳,默认现在 print (time.ctime(23)) Thu Jan 1 08:00:23 1970 time.gmtime(seconds)  #返回struct时间格式,默认格林威治时间 p

创建使用模块与datetime模块使用

为了实现对程序特定功能的调用和存储,人们将代码封装起来,可以供其它程序调用,可以称之为模块. 使用模块的好处: 程序可扩展性 减少程序代码 方便程序架构的更改 Python 自带有200多个常用模块, Python官网现在已收集了2000多个模块,你基本上可以找到你想要的任何功能 import  模块的引入 import module1[, module2[,... moduleN] 如,导入系统模块 import math,os,sys 制作模块 vim  first_module.py #!

Python下time模块和datetime模块的使用

时间模块 time模块 时间表示格式: 1.格式化时间: [ 2018-01-01 01:00:00 ] 2.时间戳: [ 1518407077.940927 ] -> 秒数(从1970开始到现今) 3.以元组方式表示,九个元素(格式化元组) 元素如下: (1) tm_year=2018, # 年 (2) tm_mon=2, # 月 (3) tm_mday=12, # 日 (4) tm_hour=11, # 小时 (5) tm_min=49, # 分钟 (6) tm_sec=13, # 秒 (7