#!usr/bin/python
# coding=UTF-8
'''
Python3 OS 常用目录操作方法
'''
'''
os.open(file, flags[, mode]) 打开一个文件,并且设置需要的打开选项,mode参数是可选的
file -- 要打开的文件
flags -- 该参数可以是以下选项,多个使用 "|" 隔开:
os.O_RDONLY: 以只读的方式打开
os.O_WRONLY: 以只写的方式打开
os.O_RDWR : 以读写的方式打开
os.O_NONBLOCK: 打开时不阻塞
os.O_APPEND: 以追加的方式打开
os.O_CREAT: 创建并打开一个新文件
os.O_TRUNC: 打开一个文件并截断它的长度为零(必须有写权限)
os.O_EXCL: 如果指定的文件存在,返回错误
os.O_SHLOCK: 自动获取共享锁
os.O_EXLOCK: 自动获取独立锁
os.O_DIRECT: 消除或减少缓存效果
os.O_FSYNC : 同步写入
os.O_NOFOLLOW: 不追踪软链接
os.getcwd() 返回当前工作路径
os.listdir(path) 返回指定路径下的文件和目录信息
os.pardir() 获取当前目录的父目录,以字符串形式显示目录名。
os.mkdir(path) 创建目录(直接括号中输入路径及新建的目录名称)
os.mkdir(path[, mode])
path -- 要创建的目录,可以是相对或者绝对路径。
mode -- 要为目录设置的权限数字模式
os.makedirs(path) 创建多级目录
os.chdir(path) 把path设置为工作目录
os.rename(src, dst) 重命名文件(src需要重命名的路径及文件,dst路径及新的文件名)
os.renames(old, new)递归地对目录进行更名,也可以对文件进行更名。
os.read(fd, n) 从文件描述符 fd 中读取最多 n 个字节,返回包含读取字节的字符串,文件描述符 fd对应文件已达到结尾, 返回一个空字符串。
os.write(fd, str) 写入字符串到文件描述符 fd中. 返回实际写入的字符串长度
os.rmdir(path) 删除path指定的空目录,如果目录非空,则抛出一个OSError异常。
os.remove(path) 删除路径为path的文件。如果path 是一个文件夹,将抛出OSError; 查看下面的rmdir()删除一个 directory。
os.removedirs(path) 删除多级目录
os.unlink(path) 删除文件路径
os.close(fd) 关闭文件描述符 fd
'''
import os,sys
path = os.getcwd()
print('当前工作路径:', path) #输出 当前工作路径: E:\Program Files\pythonProject\python\17file
print('当前父目录:', os.path.abspath(os.pardir)) #输出 当前父目录: E:\Program Files\pythonProject\python
#返回指定路径下的所有文件和目录信息
list1 = os.listdir(path)
for file in list1:
print(file,end=',')
print('')
#输出 os_common.py,os_file.py,test.txt,test1.txt,test3.txt,
#创建目录
'''
path = os.getcwd()+'/new';
os.mkdir(path, 777 )
list1 = os.listdir(os.getcwd())
for file in list1:
print(file,end=',')
'''
#输出 new,os_common.py,os_file.py,test.txt,test1.txt,test3.txt,
'''
创建多级目录
path = os.getcwd()+'/new1/new2/new3'
os.makedirs(path, 777)
list1 = os.listdir(os.getcwd())
for file in list1:
print(file,end=',')
#输出 new,new1,os_common.py,os_file.py,test.txt,test1.txt,test3.txt,
#并且在new1目录下创建了new2,在new2目录下创建了new3
'''
'''
修改工作目录
print('当前工作路径:', os.getcwd()) #输出 当前工作路径: E:\Program Files\pythonProject\python\17file
os.chdir(os.getcwd()+'/new')
print('当前工作路径:', os.getcwd()) #输出 当前工作路径: 当前工作路径: E:\Program Files\pythonProject\python\17file\new
'''
#修改文件名称
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new1', 'os_common.py', 'os_file.py', 'test.txt', 'test1.txt', 'test3.txt']
#os.rename('new1','new111')
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new111', 'os_common.py', 'os_file.py', 'test.txt', 'test1.txt', 'test3.txt']
#os.renames('test3.txt', 'new111/newtest3.txt')
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new111', 'os_common.py', 'os_file.py', 'test.txt', 'test1.txt']
#在目录new111下有newtest3.txt文件
#读写文件内容
f1 = os.open("test.txt",os.O_RDWR)
str = 'This is python write test'
w_count = os.write(f1, bytes(str, 'UTF-8'))
print('写入位数为:',w_count) #输出 写入位数为: 25
os.close(f1)
f1 = os.open("test.txt",os.O_RDWR)
r_str = os.read(f1,10)
print('读取10位:',r_str) #输出 读取10位: b'This is py'
os.close(f1)
'''
删除目录和文件
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new111', 'os_common.py', 'os_file.py', 'test.txt', 'test1.txt']
path = os.getcwd()+'/new' #空目录
os.rmdir(path)
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new111', 'os_common.py', 'os_file.py', 'test.txt', 'test1.txt']
#如果目录不存在,则抛出 FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'E:\\Program Files\\pythonProject\\python\\17file/new'
#如果目录非空,则抛出 OSError: [WinError 145] 目录不是空的。: 'E:\\Program Files\\pythonProject\\python\\17file/new'
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new111', 'os_common.py', 'os_file.py', 'test.txt', 'test1.txt']
path = os.getcwd()
os.remove(path)
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new111', 'os_common.py', 'os_file.py', 'test.txt']
#如果文件不存在,抛出 FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'E:\\Program Files\\pythonProject\\python\\17file/test1.txt'
#如果是目录,抛出 PermissionError: [WinError 5] 拒绝访问。: 'E:\\Program Files\\pythonProject\\python\\17file'
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new', 'new111', 'os_common.py', 'os_file.py', 'test.txt']
path = os.getcwd()+'/new'
os.removedirs(path)
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new111', 'os_common.py', 'os_file.py', 'test.txt']
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new111', 'os_common.py', 'os_file.py', 'test.txt']
os.unlink('test.txt')
print('当前目录文件中有:%s'%os.listdir(os.getcwd()))
#输出 当前目录文件中有:['new111', 'os_common.py', 'os_file.py']
'''
'''
os.path模块
os.path.abspath(path) 返回绝对路径
os.path.basename(path) 返回文件名
os.path.dirname(path) 返回文件路径
os.path.realpath(path) 返回真实路径
os.path.getctime(path) 返回文件 path 创建时间
os.path.getatime(path) 返回最近访问时间(浮点型秒数)
os.path.getmtime(path) 返回最近文件修改时间
os.path.getsize(path) 返回文件大小,如果文件不存在就返回错误
os.path.exists(path) 判断目录或文件是否存在,如果存在则返回True,否则返回False
os.path.isabs(path) 判断是否为绝对路径
os.path.isfile(path) 判断路径是否为文件
os.path.isdir(path) 判断路径是否为目录
os.path.split(path) 把路径分割成 dirname 和 basename,返回一个元组
os.path.splitext(path) 分割路径中的文件名与拓展名,返回一个元组
'''
path = 'test/test.txt'
print('text.txt绝对路径:', os.path.abspath(path))
print('text.txt文件名:', os.path.basename(path))
print('text.txt文件路径:', os.path.dirname(path))
print('text.txt真实路径:', os.path.realpath(path))
#输出如下:
#text.txt绝对路径: E:\Program Files\pythonProject\python\17file\test\test.txt
#text.txt文件名: test.txt
#text.txt文件路径: test
#text.txt真实路径: E:\Program Files\pythonProject\python\17file\test\test.txt
import time
print('text.txt创建时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(path))))
print('text.txt最近访问时间:', time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getatime(path))))
print('text.txt最近修改时间:', time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime(path))))
#输出
#text.txt创建时间: 2021-04-10 12:46:41
#text.txt最近访问时间: 2021-04-10 12:49:12
#text.txt最近修改时间: 2021-04-10 12:47:49
print('text.txt文件大小:', os.path.getsize(path)) #输出 text.txt文件大小: 25字节
print('text.txt文件路径分割:', os.path.split(path)) #输出 text.txt文件路径分割: ('test', 'test.txt')
print('text.txt文件名分割:', os.path.splitext(path)) #输出 text.txt文件名分割: ('test/test', '.txt')
path = 'test/test.txt'
if(os.path.exists(path)):
print(path,'文件存在')
else:
print(path, '文件不存在')
#输出 test/test.txt 文件存在
if(os.path.isabs(path)):
print(path,'是绝对路径')
else:
print(path, '不是绝对路径')
#输出 test/test.txt 不是绝对路径
path = os.getcwd()+'test/test.txt'
if(os.path.isabs(path)):
print(path,'是绝对路径')
else:
print(path, '不是绝对路径')
#输出 E:\Program Files\pythonProject\python\17filetest/test.txt 是绝对路径
path = 'test/test.txt'
if(os.path.isfile(path)):
print(path,'是文件')
else:
print(path, '不是文件')
#输出 test/test.txt 是文件
path = 'test/test.txt'
if(os.path.isdir(path)):
print(path,'是目录')
else:
print(path, '不是目录')
#输出 test/test.txt 不是目录
path = 'test'
if(os.path.isdir(path)):
print(path,'是目录')
else:
print(path, '不是目录')
#输出 test 是目录
Python3 OS 常用目录操作方法
最新推荐文章于 2023-03-03 11:06:58 发布