20 kinds of common usage Python module programming --OS

A common use OS module

In Python, OS module provides a very rich way to handle files and directories. Common usage is as follows:

1. os.getcwd()

Get the current working directory, that is, the current directory Python script work.

>>> import os
>>> os.getcwd()             #查看当前目录
'E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料'
2. os.chdir()

Change the current working directory.

>>> os.getcwd()
'E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料'
>>> os.chdir("E:\\课堂笔记")            #改变工作目录到E:\\课堂笔记
>>> os.getcwd()             #查看当前工作目录
'E:\\课堂笔记'
3. os.listdir ()

Displays all files and directories under the current directory

>>> os.chdir("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料")
>>> os.listdir()              #显示当前目录下的所有文件
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'WINDOWS理论课11.doc', 'zy.txt', '下午作 业']
4. os.path.isdir ()

Determine whether it is a directory (that is, the directory must be a folder), the return value is: True or False

>>> os.path.isdir("下午作业")          #目录——>必须是文件夹
True
5. os.path.isfile()

Determining whether a file is, the return value: True or False

  • You must be an absolute path
  • Must be a file in a directory that
>>> os.path.isfile("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\zy.txt")
True
6. os.mkdir()

New Directory

>>> os.listdir()
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'WINDOWS理论课11.doc', 'zy.txt', '下午作 业']
>>> os.mkdir("test")
>>> os.listdir()
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'test', 'WINDOWS理论课11.doc', 'zy.txt', '下午作业']
7. os.makedirs ()

New directory subdirectories

>>> os.makedirs("test2\\1\\2")
>>> os.listdir()
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'test', 'test2', 'WINDOWS理论课11.doc', 'zy.txt', '下午作业']
8. os.rmdir ()

You can only delete empty directories for the directory (ie, folder), otherwise it can not be deleted; you want to delete subdirectories of files, you can use shutil.rmtree ()

>>> os.listdir()
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'test', 'test2', 'WINDOWS理论课11.doc', 'zy.txt', '下午作业']
>>> os.rmdir("test")
>>> os.rmdir("test2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [WinError 145] 目录不是空的。: 'test2'
>>> import shutil
>>> shutil.rmtree("test2")
>>> os.listdir()
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'WINDOWS理论课11.doc', 'zy.txt', '下午作 业']
9. os.remove ()

Delete Files

>>> os.listdir()
['123.txt', 'dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域传送漏洞实验.docx', 'erjiyumingbaopo.py', 'WINDOWS理论课11.doc', 'zy.txt', '下午作业']
>>> os.remove("123.txt")
>>> os.listdir()
['dict.txt', 'dnsbrute.py', 'dnsyuming_dict.txt', 'DNS二级域名爆破.docx', 'DNS区域 传送漏洞实验.docx', 'erjiyumingbaopo.py', 'WINDOWS理论课11.doc', 'zy.txt', '下午作 业']
10. os.system ()

PowerShell command shell and run the command.

>>> os.system("cmd")
Microsoft Windows [版本 10.0.17134.765]
(c) 2018 Microsoft Corporation。保留所有权利。

E:\课堂笔记\第二十三节(Windows)(2019年6月1日)\笔记和资料>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("powershell")
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

PS E:\课堂笔记\第二十三节(Windows)(2019年6月1日)\笔记和资料>
11. os.sep

Meaning: a system main path delimiter

Windows systems is "\", Linux-like systems such as Ubuntu separator is "/", and Apple Mac OS system is ":"

>>> import os
>>> os.sep
'\\'
12. os.path.split()

Returns the directory name and file name of a path. (Must be an absolute path)

>>> os.path.split("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料")
('E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)', '笔记和资料')
13. os.path.splitdrive()

In split path disc

>>> import os
>>> os.path.splitdrive("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料")
('E:', '\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料')
14. os.path.join()

Combination path

>>> path="d:\\python_code\\csv"
>>> f="1.txt"
>>> os.path.join(path,f)
'd:\\python_code\\csv\\1.txt'
15. os.path.exists()

Used to test whether a given path is present, the return value: True or False.

>>> import os
>>> os.path.exists("E:\课堂笔记\第二十三节(Windows)(2019年6月1日)")
True
>>> os.path.exists("E:\课堂笔记\第二十四节(2019年6月2日)Windows")
True
>>> os.path.exists("E:\课堂笔记\第二十四节(2019年6月2日)Windows\\1.txt")
False
16. os.path.abcpath()

Get the absolute path. CMD can only display the directory under.

>>> os.path.abspath("下午作业")
'E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\下午作业'
17. os.path.basename()

Remove the directory path, file name returns

>>> os.path.basename("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\下午作业")
'下午作业'
18. os.path.dirname()

Remove the file name, directory path to return

>>> os.path.dirname("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\ 下午作业")
'E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料'
19. os.path.splitext()

Separate file name and extension

>>> os.path.splitext("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\ 下午作业\\zy.txt")
('E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\ 下午作业\\zy', '.txt')
>>> os.path.splitext("zy.txt")
('zy', '.txt')
20. os.path.getsize()

Get File Size, if a directory name, return 0; if the number of bytes in a file, the file is returned.

>>> os.path.getsize("E:\\课堂笔记")
8192
>>> os.path.getsize("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)")
0
>>> os.path.getsize("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料")
4096
>>> os.path.getsize("E:\\课堂笔记\\第二十三节(Windows)(2019年6月1日)\\笔记和资料\\dict.txt")
356

Guess you like

Origin blog.csdn.net/weixin_45116657/article/details/91580328