Home>Article>Backend Development> How does Python's built-in module OS create a SHELL-side file processor?

How does Python's built-in module OS create a SHELL-side file processor?

WBOY
WBOY forward
2022-10-11 16:17:00 2203browse

This article brings you relevant knowledge aboutPython, which mainly introduces the relevant content about how the built-in module OS builds a SHELL-side file processor. Let’s take a look at it together. I hope it will be helpful to you. Everyone is helpful.

How does Python's built-in module OS create a SHELL-side file processor?

[Related recommendations:Python3 video tutorial]

OS module

When explaining the package module, we mentioned To check the global package path and registration through the sys module, today we try to understand the OS module. The main function of this module is to open up the communication between the program and the system.

Through help('modules') we can find that OS appears as a built-in module of python.

How does Pythons built-in module OS create a SHELL-side file processor?

With path

Judge the file

import os print(os.path.isfile('demo.txt'))

If it is a file, return True Otherwise returns False.

Judge the folder

import os print(os.path.isdir('../os'))

We pass a path as the parameter. If it is a folder, it returns True, otherwise it returns False.

Determine whether the file exists

This should be an operation we often use in scripts. If it exists, we will use it. If it does not exist, we need to create the file. Populate default content.

import os print(os.path.exists('ttttt.txt'))

Similarly returns True if it exists and False otherwise.

Get the file size

We often view file information through ls -al on the server, including the file size attribute, as python The built-in modules must also have related attribute acquisition functions.

import os print(os.path.getsize('demo.txt'))

Getting the path and file

How does Pythons built-in module OS create a SHELL-side file processor?

#In the past, when operating Java, we often needed to get the directory where the file is located. At that time, it was all obtained through Java object attributes, and Python China Automated took care of it for us.

Get the absolute path

I wonder if you have noticed that when we get the directory where the script is located above, we use ../os and other methods, including the path and file chapter where we manually type out the file. The full path is actually provided by python.

import os print(os.path.abspath('practice.py'))

How does Pythons built-in module OS create a SHELL-side file processor?

Return file name

import os print(os.path.basename('practice.py'))

How does Pythons built-in module OS create a SHELL-side file processor?

Get file path

How does Pythons built-in module OS create a SHELL-side file processor?

It only works if we pass in the full path of the file dirname, which feels a bit tasteless

Path splicing

We often have paths Address concatenation. Windows and Linux have different file delimiters. Java provides us with a variable to obtain the delimiter, but python directly ignores the delimiter and directly provides us with a splicing method.

import os print(os.path.join('parent','child'))

How does Pythons built-in module OS create a SHELL-side file processor?

Without path

Get the platform

Sometimes we need To perform different operations according to different platforms, you need to obtain platform information at this time.

import os print(os.name)

How does Pythons built-in module OS create a SHELL-side file processor?

Get all files

Since it is a module, our commonly used functions are definitely indispensable. As mentioned before, we often use the server Execute ls -al .

import os print(os.listdir)

How does Pythons built-in module OS create a SHELL-side file processor?

Current working path

Many times we do not return directly Script path execution scripts are often executed through absolute paths or relative paths. Sometimes scripts will depend on certain files on the execution path, so it is important to obtain the execution script path.

How does Pythons built-in module OS create a SHELL-side file processor?

Directory operations

I believe it is also important to create and delete files and directories. Let’s take a look at how to do it.

import os os.mkdir('test') print(os.getcwd()+'工作下的文件列表'+os.listdir()) os.rmdir('test')

How does Pythons built-in module OS create a SHELL-side file processor?

Rename File

import os os.rename(old,new)

File Properties

How does Pythons built-in module OS create a SHELL-side file processor?

我们能够看到打印了很多属性。

How does Pythons built-in module OS create a SHELL-side file processor?

How does Pythons built-in module OS create a SHELL-side file processor?

修改权限杀死进程

os.chmod(file) os.get_terminal_size() os.kill(10884,signal.SIGKILL)

打通shell

作为一个shell爱好者,我还是很喜欢使用shell 来实现的,尤其是在做系统初始化的时候这个时候没有python ,而shell 是linux 系统自带的,所以shell 脚本的时候还是很有必要的,我个人也是shell+python 相互辅佐的存在。我们知道shell 中直接 python xxx.py。 但是python 如何执行shell 呢?

import os name=os.system('ls -al')

上面我们提到获取平台信息 os.name ,我们可以根据这个命令来通过 os.system('cmd') 指定不同系统的cmd 命令。

【相关推荐:Python3视频教程

The above is the detailed content of How does Python's built-in module OS create a SHELL-side file processor?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete