search
HomeBackend DevelopmentPython TutorialHow does Python's built-in module OS create a SHELL-side file processor?

This article brings you relevant knowledge about Python, 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:掘金. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software