Python的os模块包含普遍的操作系统功能。如果希望程序能够与平台无关,这个模块是尤为重要。可以处理文件和目录这些我们日常手动需要做的操作
OS模块中常用属性和函数:
1、os.getcwd()
查看当前所在目录(路径),即当前Python脚本工作的目录路径。
2、os.listdir()
返回指定目录下的所有文件和目录名。
>>> os.listdir(os.getcwd()) ['Django', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'MySQL-python-wininst.log', 'NEWS.txt', 'PIL-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'RemoveMySQL-python.exe', 'RemovePIL.exe', 'Removesetuptools.exe', 'scripts', 'setuptools-wininst.log', 'tcl', 'Tools', 'w9xpopen.exe']
3、os.remove()
删除文件,不能删除文件夹;
>>> import os
>>> print(os.listdir('/test'))
['123', 'hello.txt', '.file', 'qwe']
>>> os.remove('/test/.file')
>>> print(os.listdir('/test'))
['123', 'hello.txt', 'qwe']
>>> os.remove('/test')
Traceback (most recent call last):
File "", line 1, in
IsADirectoryError: [Errno 21] Is a directory: '/test'
4、os.system()
运行shell命令,直接显示(相当于启动一个全新的shell,然后去执行那条命令,命令执行完成过后,shell直接退出);
>>> import os
>>> print(os.system('ls /test/')) #调用系统命令
123 hello.txt qwe
0
[root@server-7 test]# ls
123 hello.txt qwe
5、os.path.split(path)
将path分割成目录和文件名二元组返回;
>>> import os
>>> print(os.path.split('/test/123/abc'))
('/test/123', 'abc')
6、os.path.splitext():分离文件名与扩展名
>>> os.path.splitext('a.txt')
('a', '.txt')
7、os.path.exists(path)
判断路径是否存在,如果path存在,返回True;如果不存在,返回Flase;
>>> import os
>>> print(os.path.exists('/test/123/abc'))
True
8、os.path.join(path1,[path2],[path3])
将路径和文件名分为一个列表中的两个元素,将它们拼起来,若其中有绝对路径,则之前的path将会被删除.;
>>> import os
>>> print(os.path.join('/test/','hello.txt'))
/test/hello.txt
>>> print(os.path.join('/test/123','hello.txt'))
/test/123/hello.txt
9、os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。
>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile('a.txt')
False
10、os.path.exists()函数用来检验给出的路径是否真的存在
>>> os.path.exists('C:\Python25\abc.txt')
False
>>> os.path.exists('C:\Python25')
True
11、os.makedirs() 生成一个多层递归目录
>>> import os
>>> os.makedirs('/home/test/xuan')
12、os.removedirs()
若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依次类推;
>>> import os
>>> os.removedirs('test/xuan')
13、os.mkdir()
创建一个目录;
>>> import os
>>> os.mkdir('test') #只能创建一个目录
>>> os.mkdir('abc/123/xxx')
Traceback (most recent call last):
File "", line 1, in
FileNotFoundError: [Errno 2] No such file or directory: 'abc/123/xxx'
结果:
[root@server-7 home]# tree /home/
/home/
└── test
14、os.rmdir()
删除一个目录,若目录不为空则无法删除,报错;
>>> import os
>>> os.mkdir('test/abc') #在test下创建一个abc;
>>> os.rmdir('test') #无法删除test;
Traceback (most recent call last):
File "", line 1, in
OSError: [Errno 39] Directory not empty: 'test'
>>> os.rmdir('test/abc') #可以删除abc,因为abc目录下为空;
>>>
15、os.listdir()
显示指定目录下,所有的文件和子目录,包括隐藏文件;
>>> import os
>>> dirs = os.listdir('/root')
>>> print(dirs)
['.bash_logout', '.bash_profile', '.cshrc', '.tcshrc', '.bashrc', 'full_stack', 'jf_blog', 'node-v8.9.4-linux-x64', '.bash_history', '.config', '.npm', '.pki', '.ssh', '.gitconfig', '.oracle_jre_usage', '.cache', '.python_history', 'douban', 'mysql57-community-release-el7-10.noarch.rpm', '.mysql_history', '.viminfo']
16、os.path.basename(path)
返回path最后的文件名(一个绝对路径只返回最后的文件名);
>>> import os
>>> print(os.path.basename('abc'))
abc
>>> print(os.path.basename('/test/123/abc'))
abc
17、os.path.dirname(path)
返回path的目录;
>>> import os
>>> print(os.path.dirname('/test/123/abc'))
/test/123
18、os.path.abspath(path)
返回path规范化的绝对路径;
>>> import os
>>> print(os.path.abspath('abc'))
/test/123/abc
原文链接:https://blog.csdn.net/weixin_44897792/article/details/95455084



