Python——OS模块中的常用法
目录
一、os模块中的方法
os.rename()——文件、文件目录的重命名
os.remove()——删除文件
os.rmdir()、os.removedirs()——删除目录
os.mkdir()——创建目录
os.cwd()——获取当前目录
os.chdir()——更改目录
os.listdir——查看当前目录的文件及目录
例子
将a文件的内容复制到b文件中
二、os.path模块中的方法
os.path.abspath('')——打印绝对路径
os.path.join()——拼接目录
os.path.split('')——拆分目录
os.path.isdir()——查看字符串是否为目录
os.path.exist('')——查看目录或文件是否存在
os.path.isfile(''):查看某文件是否实际存在
os.path.getsize(''):返回字节数
os.path.dirname:获取文件所在目录
os.path.getatime(''):获取最后访问时间
os.path.getctime(''):获取创建时间
os.path.getmtime(''):获取修改时间
os.path.isabs(''):是否为绝对路径
Python的OS模块可以完成对于文件的增删改查功能,以下是一些常用方法:
一、os模块中的方法
os.rename()——文件、文件目录的重命名
语法:
os.rename(old_file_name/path,new_file_name/path)
os.renames(old_file_name/path,new_file_name/path)
很好理解,将旧的文件名或者目录改为新的,文件内容不变,更改目录时候同理,目录中的文件不变,区别如下:
假设有这样几个目录和文件
os.rename('a/b/1','a/c/2')
os.renames('a/bb/11','a/cc/22')
rename 会直接报错找不到路径,而 renames 运行后会直接把目录名和整个目录结构更改,是重命名,不是新建
os.remove()——删除文件
只能删除文件,不能删除目录,会报拒绝访问的错误
os.remove('path/filename')
os.rmdir()、os.removedirs()——删除目录
os.rmdir('path')
os.removedirs('path')
注意点:
os.mkdir()——创建目录
注意点:
os.cwd()——获取当前目录
print(os.getcwd())
D:\mypythonfile
os.chdir()——更改目录
os.chdir('/mypythonfile/a/b')
print(os.getcwd())
D:\mypythonfile\a\b
os.listdir——查看当前目录的文件及目录
可以看到,目录下有一些文件夹和一个 t.py 文件,返回的是列表类型
print(os.listdir(os.getcwd()))
['.idea', 'a', 'demo1', 'Ivalley', 'newpy', 'pyspark', 't.py']
例子
将a文件的内容复制到b文件中
- 使用只读的方式打开file1文件
- 将读取到的结果写入到file2文件中
file_1=open('/mypythonfile/a/bb/file1','r',encoding='utf-8')
file_2=open('/mypythonfile/a/bb/file2','w',encoding='utf-8')
f=file_1.read()
# read里面的整数表示一次性读取的字符数,光标逐个移动往后移动
print(f)
file_2.write(f)
file_1.close()
file_2.close()
二、os.path模块中的方法
os.path.abspath('')——打印绝对路径
import os.path
print(os.path.abspath(''))
D:\mypythonfile\os_path
os.path.join()——拼接目录
p2开头注意不能有 \,不然会直接找到根目录
p1='a'
p2='b'
print(os.path.join(p1,p2))
a\b
os.path.split('')——拆分目录
按 \ 切分,前面的 \\ 有一个是转义符,结果返回元组,与 os.path.splittext 的区别:os.path.splittext 获取文件扩展名,用 . 分割字符串(如 .txt 和 .py 等等,文件带有 . 号的可能会出错)
print(os.path.split('D:\mypythonfile\os_path\os_path.py'))
print(os.path.splitext('D:\mypythonfile\os_path\os_path.py'))
('D:\\mypythonfile\\os_path', 'os_path.py')
('D:\\mypythonfile\\os_path\\os_path', '.py')
os.path.isdir()——查看字符串是否为目录
print(os.path.isdir('D:\mypythonfile\os_path'))
print(os.path.isdir('D:\mypythonfile\os_path\os_path.py'))
True
False
os.path.exist('')——查看目录或文件是否存在
print(os.path.exists('D:\mypythonfile\os_path\os_path.py'))
print(os.path.exists('D:\mypythonfile\os_path'))
True
True
os.path.isfile(''):查看某文件是否实际存在
print(os.path.isfile('D:\mypythonfile\os_path\os_pa.py'))
print(os.path.isfile('D:\mypythonfile\os_path\os_path.py'))
print(os.path.isfile('D:\mypythonfile\os_path'))
False
True
False
os.path.getsize(''):返回字节数
中文、换行符按3个字节算,英文、空格按1个字节算,如对于文件:
print(os.path.getsize('D:\mypythonfile\os_path\gs'))
6
os.path.dirname:获取文件所在目录
不在乎路径或文件是否存在,只是字符串的处理,如对于文件目录:
print(os.path.dirname('D:\mypythonfile\os_path\gs'))
print(os.path.dirname('D:\mypythonfile\os_path\myfile.py'))
D:\mypythonfile\os_path
D:\mypythonfile\os_path
函数会按最后一个 \ 进行分割返回结果
print(os.path.dirname('D:\mypythonfile\os_path'))
print(os.path.dirname('D:\mypythonfile\os_path\\'))
# 上面的 \\ 是转译后结尾加的 \
D:\mypythonfile
D:\mypythonfile\os_path
os.path.getatime(''):获取最后访问时间
os.path.getctime(''):获取创建时间
os.path.getmtime(''):获取修改时间
几个函数语法一样,返回的是时间戳,可以使用 time 模块进行处理
时间对象及 time模块方法参见:
python的time模块_python time模块-CSDN博客
import os.path
import time
print(os.path.getatime('D:\mypythonfile\os_path\gs'))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getatime('D:\mypythonfile\os_path\gs'))))
print(os.path.getctime('D:\mypythonfile\os_path\gs'))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getctime('D:\mypythonfile\os_path\gs'))))
print(os.path.getmtime('D:\mypythonfile\os_path\os_path.py'))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getmtime('D:\mypythonfile\os_path\gs'))))
1730080767.2549422
2024-10-28 09:59:27
1730080170.5559597
2024-10-28 09:49:30
1730084136.8645022
2024-10-28 09:59:27
os.path.isabs(''):是否为绝对路径
这个函数接受字符串参数,也只是对字符串的判断
作者:带带琪宝