Python os模块常用函数
1. os.getcwd():获取当前的工作目录
getcwd()方法语法格式如下:os.getcwd()
import os, sys
print("当前工作目录 : %s" % os.getcwd())
当前工作目录 : F:\Proiect\Python-Learning\python\path
2. os.listdir(): 用于返回指定文件夹包含的文件或文件夹的名字的列表
import os, sys
path = 'F:/Proiect/Python-Learning/python'
dirs = os.listdir(path)
for file in dirs:
print(file)
.idea
Data
file
hello.txt
Matplotlib
matplotlib.xmind
numpy
numpy.xmind
Pandas
path
Python_base
类与对象
3. os.path.abspath(path) :返回绝对路径
import os, sys
path1 = os.path.abspath(r".")
print(path1)
path2 = os.path.abspath(r"..")
print(path2)
F:\Proiect\Python-Learning\python\path
F:\Proiect\Python-Learning\python
4.os.system() :运行shell命令
import os, sys
os.system('cmd')
os.system('ls')
5. os.path.split() :将路径和文件名分开
import os, sys
path = os.path.split("F:/Proiect/Python-Learning/python/path/demo.py")
print(path)
('F:/Proiect/Python-Learning/python/path', 'demo.py')
6. os.path.join():连接两个或更多的路径名组件
import os
Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
Path10 = homedevelopcode
Path20 = home\develop\code
7. os.path.dirname():去掉文件名,返回目录
import os
path = "F:/Proiect/Python-Learning/python/path/demo.py"
print(os.path.dirname(path))
F:/Proiect/Python-Learning/python/path
8. os.path.basename():返回path最后的文件名。如果path以/或\结尾,那么就会返回空值。
import os
path = "F:/Proiect/Python-Learning/python/path/demo.py"
path1 = os.path.basename(path)
print(path1)
demo.py
9. os.mkdir():只创建一级目录
import os
path = "F:/Proiect/Python-Learning/python/path"
path1 = os.mkdir(path + './file1')
10. os.makedirs():创建多级目录
import os
path = r"F:/Proiect/Python-Learning/python/path"
os.makedirs(path + "./file1"+ "./file1_1" + "./file1_1_1")
11. os.remove():删除文件
12. os.chdir(path):用于改变当前工作目录到指定路径
import os
path = './file1'
retval = os.getcwd()
print("当前工作目录为:\t", retval)
os.chdir(path)
retval = os.getcwd()
print("目录修改成功:\t", retval)
当前工作目录为: F:\Proiect\Python-Learning\python\path
目录修改成功: F:\Proiect\Python-Learning\python\path\file1
13. os.path.getmtime(path), os.path.getatime(path), os.path.getctime(path) 查看时间
import os
import time
file = "F:/Proiect/Python-Learning/python/path/demo.py"
Now_time= os.path.getatime(file)
print("Now_time:\t", Now_time)
crate_time = os.path.getctime(file)
print("crate_time:\t", crate_time)
Modify_time = os.path.getmtime(file)
print("Modify_time:\t", Modify_time)
time.gmtime(os.path.getmtime(file))
file_size = os.path.getsize(file)
print("file_size:\t", file_size)
abs_path = os.path.abspath(file)
print("file_size:\t", abs_path)
norm_path = os.path.normpath(file)
print("norm_path:\t", norm_path)
Now_time: 1605593018.7174108
crate_time: 1605572830.3529973
Modify_time: 1605593018.7174108
file_size: 1174
file_size: F:\Proiect\Python-Learning\python\path\demo.py
norm_path: F:\Proiect\Python-Learning\python\path\demo.py
14. os.path.exists(path) 、os.path.isfile(path)、os.path.isdir(path)查看文件
import os
path = "F:\Proiect\Python-Learning\python\path\demo.py"
path1 = "F:\Proiect\Python-Learning\python\path"
exists = os.path.exists(path)
isfile = os.path.isfile(path)
isdir = os.path.isdir(path1)
print("exists:\t", exists)
print("isfile:\t", isfile)
print("isdir:\t", isdir)
exists: True
isfile: True
isdir: True