Python (5) os and sys modules of common modules

 1. os module

1.os.name

The output string indicates the platform in use. 'nt' for window, 'posix' for Linux/Unix users.

2.os.getcwd()

The function gets the current working directory, that is, the directory path where the current Python script works.

3.os.listdir('D:\python')

List all files in the specified directory.

4.os.remove(‘test.txt’)

delete a file, only delete files

5.os.rmdir('test')

Delete a folder, only delete folders

6.os.removedirs('python/code')

Delete folders recursively, note that only empty directories can be deleted

7.os.mkdir ('mll')

Create folder

8.os.makedirs ('mll / test')

Create a folder recursively. When creating a folder, if the parent directory does not exist, it will automatically create the parent directory for you

9.os.sep

Get the path separator of the current operating system

10.os.rename('test1','test2')

Rename the file test1 to test2

11.os.stat('homework1.py')

Get file information

12.os.linesep

Get the newline character of the current operating system

13.os.pathsep

The separator for each path in the environment variables of the current system, windows is ;, linux is:

14.os.environ

environment variables for the current system

15.os.system('ipconfig')

Start dos, execute operating system commands, but get no results

res = popen('ipconfig').read()

print(res)

16.os.path.abspath(file)

Get the absolute path of the file

17.os.path.split('D:/syz_python/code/day6/review.py')

 Split path and filename

18.os.path.dirname('D:/syz_python/code/day6')

Get the parent directory, get his parent directory

19.os.path.basename('D:/syz_python/code/day6')

 Get the last level, if it is a file, display the file name, if it is a directory, display the directory name,

20.os.path.exists('D:/syz_python/code/day6')

Whether the file/directory exists, returns True if it exists, and returns False if it does not exist

21.os.path.isabs('D:/syz_python/code/day6')

Determine whether it is an absolute path, if yes, return True, if not, return False

22.os.path.isfile(''xiaohei.py")

To determine whether it is a file: 1. The file must exist; 2. It must be a file. Return True if yes, False if not

 23.os.path.isdir('D:/syz_python/code')

Determine whether it is a path and whether the path exists. Return True if yes, False if not

24.os.path.getsize('homework.py')

get file size

25.os.path.join('D:/','python/','mll','test.py')

concatenate into a path

 

26.os.walk(r'D:\syz_python\code\day6\test')

 Get the contents of the directory

abs_path the absolute path of the current loop

All folders under the dir directory [ ]

All files under the file directory [ ]

 2. sys module

1.sys.path

path is a list of directories for Python to look for third-party extension modules from. When python starts, sys.path is initialized according to the built-in rules and the PYTHONPATH variable.

2.sys.path.insert(0,'test')

Insert test at the beginning of the path, and then import test will not report an error. For example, if the other module is introduced, an error will be reported because it is not added to the environment variable.

3.os.path.append('a.py')

It is also about introducing python files into python environment variables, but inserting them at the end

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782231&siteId=291194637