Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions commonly used when writing shell scripts in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Use Python to write shell scripts often use which functions, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

In the practical application of computer language, the process of writing shell script with Python requires related functions. In the following article, we will mention the specific application of this function in Python writing shell script. The following is the specific introduction of the article.

Although I am deeply influenced by * nix, I hate the sh scripts that can be found everywhere in * nix. Why do you hate it? First of all, because of the inexplicable syntax of the sh script, it feels like a write-and-throw program, without any beauty at all. Secondly, the processing power of sh script is still relatively weak, in text processing, XML processing and network programming, basically with the help of a bunch of programs such as perl,awk.

I also don't like these programs, and it always takes time to learn third-party software, so you might as well use python. So, can python do shell scripts? Let's start with a function:

Os.system (command)

This function can call shell to run the command line command and return its return value. Try typing os.system ("ls-l") in the python interpreter and you can see that "ls" lists the files in the current directory. It can be said that through this function, python has all the capabilities of shell. However, usually this command does not need to be used. Because the commands commonly used in shell are usually written in python in a corresponding and equally concise way.

The most commonly used command in shell is the ls command. The corresponding word for python is: os.listdir (dirname). This function returns a list of strings containing all the file names, but not "." And "..". It's a little more complicated if you want to traverse the entire directory. Let's talk about it later. Try it in the interpreter first:

> > os.listdir ("/")

['tmp',' misc', 'opt',' root', '.autorelabel'

'autofsck', 'sbin',' srv',', 'mnt',' usr', 'var'

'etc', 'selinux',' lib', 'net',' lost+found', 'sys'

'media', 'dev',' proc', 'boot',' home', 'bin']

Just like this, all the following commands can be run directly in the python interpreter to see the results.

Corresponding to the cp command is: shutil.copy (src,dest), this function takes two arguments, the parameter src refers to the name of the source file, and the parameter dest refers to the name of the target file or the target directory. If dest is a directory name, a file with the same name will be created in that directory. Similar to the shutil.copy function is shutil.copy2 (src,dest), but copy2 also replicates * * access time and * * update time.

An introduction to the powerful functions of Python Standard Library

The procedure of generating dll-independent exe with Python is simple.

Introduction to the relevant steps needed to build a Python Web environment

It is necessary to pay attention to the modification of Bug and .jam files for getting started with Python.

Use Python script to convert to windows executable program

However, the cp command of shell can also copy directories, but not shutil.copy of python. * parameters can only be one file. What are we going to do? In fact, python also has a shutil.copytree (src,dst [, symlinks]). There is an extra symlinks for the parameter, and writing a shell script in Python is a Boolean value, and if it is True, create a symbolic link.

Move or rename files and directories? Probably guessed by smart friends, shutil.move (src,dst), hehe. Similar to the mv command, if src and dst are on the same file system, shutil.move simply changes the name. If src and dst are on different file systems, shutil.move will first copy src to dst, and then delete the src file. By now, most of my friends should have some idea of the ability of python. The above is the introduction of how to write shell scripts in Python. I hope you will get something.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report