Introduction to functions frequently used when writing shell scripts in Python

Source: Internet
Author: User
Tags call shell

In the practical application of computer languages, using Python to write shell scripts requires related functions, in the following article, we will mention the specific application of this function in writing shell scripts in Python. The following is a detailed introduction of this article.

Although I am deeply influenced by * nix, it is annoying for sh scripts that are everywhere in * nix. Why? First of all, it is because of the inexplicable syntax of the sh script. It feels like a program that is flushed along with writing. There is no beauty at all. Second, the processing capability of sh scripts is still relatively weak. In terms of text processing, XML processing, and network programming, a bunch of programs such as perl and awk are basically required.

I also don't like these programs very much. Besides, it takes time to learn third-party software. It's better to use python. Can python be used as a shell script? First, we will introduce a function:

 
 
  1. os.system(command) 

This function can call shell to run the command line command and return its return value. Enter OS. system ("ls-l") in the python interpreter to list the files in the current directory. It can be said that through this function, python has all the capabilities of shell. However, this command is usually not required. Because the commands commonly used by shell are usually corresponding and concise in python.

The most common shell is the ls command, and the corresponding method of python is: OS. listdir (dirname). This function returns the string list, which contains all file names but does not contain ". "and ".. ". If you want to traverse the entire directory, it will be a little more complicated. Let's wait. First, try the following in the interpreter:

 
 
  1. >>> os.listdir(”/”)  
  2. [’tmp’, ‘misc’, ‘opt’, ‘root’, ‘.autorelabel’, 
    ’sbin’, ’srv’, ‘.autofsck’, ‘mnt’, ‘usr’, ‘var’, 
    ‘etc’, ’selinux’, ‘lib’, ‘net’, ‘lost+found’, ’sys’,
     ‘media’, ‘dev’, ‘proc’, ‘boot’, ‘home’, ‘bin’] 

In this way, all subsequent commands can directly run the viewing results in the python interpreter.

Corresponding to the cp command: shutil. copy (src, dest). This function has two parameters: the src Parameter refers to the name of the source file, and the dest parameter refers to the name of the target file or the target directory of the target file. If dest is a directory name, a file with the same name will be created under that directory. Similar to the shutil. copy function, the shutil. copy2 (src, dest) function copies the last access time and last update time.

  • Introduction to the powerful functions of the Python standard library
  • Simple Procedure for generating dll-independent exe using Python
  • Steps required to build a Python Web environment
  • Note the Bug modification and. jam file when getting started with Python
  • Convert a Python script to an executable program in windows

However, the shell cp command can also copy directories, but the python shutil. copy command cannot. The first parameter can only be a file. What should I do? Python also has a shutil. copytree (src, dst [, symlinks]). The parameter has one more symlinks, and the shell script written in Python is a Boolean value. If it is True, a symbolic link is created.

What about moving or renaming files and directories? It is estimated that shutil. move (src, dst) was guessed by a smart friend .. Similar to the mv command, if src and dst are on the same file system. move is just a simple change of 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. As you can see, most of my friends have begun to look at python's capabilities. The above is an introduction to how to use Python to write shell scripts. I hope you will get something.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.