Use python to implement the Linux Shell Function

Source: Internet
Author: User

1. There are two methods to execute commands in the command line: here we take the LS commands commonly used in Linux as an example,

(1) OS. System ('LS-l ')

(2) Import subprocess

Subprocess. Call ('LS-l'. Split ())

OS. chkdir (PATH) is converted to the directory path.

OS. System ('md A') can directly create a directory.

The OS. Name string indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix '.
● The OS. getcwd () function obtains the current working directory, that is, the directory path of the current Python script.
● The OS. getenv () and OS. putenv () functions are used to read and set environment variables respectively.
● OS. listdir () returns all files and directory names under the specified directory.
● The OS. Remove () function is used to delete an object.
● The OS. System () function is used to run shell commands.
● The OS. linesep string provides the row Terminator used by the current platform. For example, in windows, '\ r \ n' is used, in Linux,' \ n' is used, and in MAC, '\ R' is used '.
● The OS. Path. Split () function returns the directory name and file name of a path.
>>> OS. Path. Split ('/home/swaroop/byte/code/poem.txt ')
('/Home/swaroop/byte/Code', 'poem.txt ')
● The OS. Path. isfile () and OS. Path. isdir () functions verify whether the given path is a file or a directory. Similarly, the OS. Path. exists () function is used to check whether the GIVEN PATH actually exists.

File redirection

If the py file new1.py already exists, enter new1> new.txt in the command line to export the result of new1runtime to new.txt, which is called Stream redirection.

 

Can Python be used as shell scripts? First, we will introduce a function:

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. Haha .. 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:
>>> OS. listdir ("/")
['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.

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 an additional symlinks, which 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 may already have an eye on Python's capabilities. Next I will list a table and introduce other functions:

OS. chdir (dirname)
Switch the current working directory to dirname

OS. getcwd ()
Returns the current working directory path.

OS. chroot (dirname)
Use dirname as the root directory of the process. Similar to the chroot command in * nix

OS. chmod (path, Mode)
Change the path permission bit. Mode can be a combination of the following values (OR:
OS. s_isuid
OS. s_isgid
OS. s_enfmt
OS. s_isvtx
OS. s_iread
OS. s_iwrite
OS. s_iexec
OS. s_irwxu
OS. s_irusr
OS. s_iwusr
OS. s_ixusr
OS. s_irwxg
OS. s_irgrp
OS. s_iwgrp
OS. s_ixgrp
OS. s_irwxo
OS. s_iroth
OS. s_iwoth
OS. s_ixoth
What are the meanings of them? I will not elaborate on them. Basically, R represents read, W represents write, and X represents execution permission. USR indicates the user, GRP indicates the group, and oth indicates others.

OS. chown (path, uid, GID)
Change the owner of a file. When UID and GID are-1, the original owner is not changed.

OS. Link (SRC, DST)
Create a hard connection

OS. mkdir (path, [mode])
Create a directory. For more information about the meaning of mode, see OS. chmod (). The default value is 0777.

OS. makedirs (path, [mode])
Similar to OS. mkdir (), however, a non-existing parent directory is created first.

OS. readlink (PATH)
Returns the path to which the symbolic link points.

OS. Remove (PATH)
Deleting a file cannot be used to delete a directory.

OS. rmdir (PATH)
Deleting folders cannot be used to delete objects.

Removedirs (PATH)
Recursively remove directories. Similar to rmdir (), removedirs () tries to delete each parent directory mentioned in path successively except if the subdirectory is successfully deleted, until an error is suspended (this error is ignored because it usually means that the parent directory is not empty .) For example, "OS. removedirs ('foo/BAR/Baz') "will first Delete the" 'foo/BAR/Baz' "directory, then, if "'foo/bar'" and "'foo'" are empty, delete them. If the subdirectory cannot be successfully deleted, the oserror is suspended. The new method in Version 1.5.2.

Rename (SRC, DST)
Rename the file or directory SRC to DST. If DST is a directory, oserror will be suspended. On UNIX, if DST exists and is a file, it will be silently deleted if the user has the permission. In some Unix-style systems, if SRC and DST are different file systems, this operation may fail. If successful, renaming is a basic operation (which is a POSIX requirement ). In Windows, if DST already exists, or even if it is a file, oserror will be suspended. When DST names an existing file, there is no way to rename it.

Renames (old, new)
Recursively rename a directory or file function. Similar to rename (), except for the creation of all the Middle-layer directories, the first attempt to create a new path name is required. After the name is renamed, the rightmost path part of the directory that matches the old name will be deleted with removedirs. The new method in Version 1.5.2.

Note: If you do not have the permission to delete sub-directories or files, this function fails to be renamed using the new directory structure.

Readlink (PATH)
Returns a string representing the path pointed to by the symbolic connection point. The result can be either an absolute or relative path name. If it is relative, it can use OS. path. join (OS. path. dirname (PATH), result) is converted into an absolute path. Available: Macintosh, UNIX.

Stat (PATH)
Execute Stat () system call on a given path. The returned value is an object whose attributes correspond to the number of stat structures, namely st_mode (Protection block), st_ino (number of index nodes), st_dev (device), st_nlink (hard connections ), st_uid (owner's user ID), st_gid (owner's group ID), st_size (file size, in bytes), st_atime (current access time ), st_mtime (time when the current content is modified), st_ctime (platform dependency, time when the current metadata is changed on UNIX, time when it was created on Windows ):

>>> Import OS

>>> Statinfo = OS .stat('somefile.txt ')

>>> Statinfo

(33188,422 10000l, 769l, 1, 1032,100,926 L, 1105022698,1105022732, 1105022732)

>>> Statinfo. st_size

926l

OS. symlink (SRC, DST)
Create a symbolic link

Tempnam ([dir [, prefix])
Returns a unique path name for creating a temporary file. This will be an absolute path named after possible directory items in the Dir directory, or the location of a common temporary file, if the Dir is ignored or none. If the Delimiter is set to none, the prefix is used to provide a brief prefix for the file name. The application is responsible for using the paths returned by tempnam () to properly create and manage files. Automatic cleanup is not provided. On UNIX, the environmental variable tmpdir overwrites Dir, and TMP is used on Windows. The specified behavior of this function depends on the execution of the C library; some aspects are underspecified in system documentation. note: The use of tempnam () is dangerous for symbolic connection attacks; Use tmpfile () (14.1.2) instead. Available:
Macintosh, UNIX, and windows.

Tmpnam ()
Returns a unique path name for creating a temporary file. This will be an absolute path, named after possible directory items in a location where a common temporary file is located, and the application is responsible for properly creating and managing files using the path returned by tempnam; automatic cleanup is not provided. Note: The use of tempnam () is dangerous for symbolic connection attacks; Use tmpfile () (14.1.2) instead. Available: Macintosh, UNIX, and windows. This function is probably not used on Windows. However, the implementation of Microsoft tmpnam () has been created to create a name in the root directory of the current driver, it is usually a rough location of a temporary file (depending on the privilege, you can't even open a file using this name ).

Tmp_max
Before using these names, tmpnam () will generate the maximum number of unique names.

Unlink (PATH)
Delete the file path. Same as remove (); unlink () name is its traditional UNIX name. Available: Macintosh, UNIX, and windows.

Utime (path, times)
Set the access and modification time of the file specified by path. If times is none, the file access and modification time are set to the current time. Otherwise, times must be a binary number, and its format used to set access and modification time is (atime, mtime ). Whether or not the directory depends on the operating system as a file execution directory (for example, windows is not ). Note that the exact time you set here cannot be returned through the subsequent Stat () call, depending on the access and modification time officially recorded by your operating system; see Stat (). Change in version 2.0: added support for "times" as "NONE. Available: Macintosh,
UNIX and windows.

Walk (top [, topdown = true [, onerror = none]), It OS. the obvious difference between listdir () is that it can traverse in depth, OS. listdir () can only traverse all subdirectories and files in the current directory.
In the directory tree, walk () generates a file name and traverses the tree from top to bottom or right down. The root of each directory in this tree is in the top directory (including the top itself), which provides a triple (dirpath, dirnames, filenames ).

Dirpath is the path string of a directory. Dirnames is a list of the names of dirpath sub-directories (excluding '.' and '..'). Filenames is a list of file names without directories in dirpath. Note that the name in the list contains a component without a path. Obtain the complete path of the files and directories in dirpath (starting with top), and use OS. Path. Join (dirpath, name ).

If the optional parameter topdown is true or not specified, the three times before its subdirectory is generated (the directory is generated from top to bottom ). If topdown is false, a three-fold directory is generated after three times its sub-directories are generated (directory generation goes down from bottom to top ).

When topdown is true, the caller can modify the original dirnames list (DEL or slice tasks may be used), and walk () only uses the subdirectories where recursive names are still in dirnames; this can be used to delete the search, use the specified sequence of access, or tell the walk () about the directory created or renamed by the caller before the walk () operation. When topdown is set to false, it is invalid to modify dirnames, because the directory in dirnames is generated before dirpath is generated in bottom-up mode.

The default OS. listdir () call error is ignored. If the optional onerror parameter is specified, it should be a function; it will be called with a parameter, an oserror instance. It can continue reporting errors with the walk, or ignore the walk suspension exception. Note that the file name is available as the file name attribute of the exception object.

Note: If you pass a relative path name, the current working directory is not changed between the start and the walk. Walk () Never changes the current directory, and it is assumed that its callers do not change.

Note: on systems that support symbolic connections, connect to the subdirectory that appears in the dirnames list, but walk () they will not be accessed (it is difficult to avoid an infinite loop when a symbolic connection is followed ). To access the Connected Directories, you can use OS. Path. islink (PATH) to identify them and directly call walk (PATH ).

This example shows the number of bytes of non-directory files in each directory in the Start directory, except for not finding any CVS sub-directory:

Import OS

From OS. Path import join, getsize

For root, dirs, files in OS. Walk ('python/lib/e-mail '):

Print root, "consumes ",

Print sum (getsize (join (root, name) for name in files ),

Print "bytes in", Len (files), "non-directory Files"

If 'cvs 'in dirs:

Dirs. Remove ('cvs ') # Don't visit CVS Directories

In the following example, traversing the tree from bottom to top is equal to: The rmdir () directory is empty and cannot be deleted:

# Delete everything reachable from the directory named in 'top ',

# Assuming there are no symbolic links.

# Caution: This is dangerous! For example, if Top = '/', it

# Cocould delete all your disk files.

Import OS

For root, dirs, files in OS. Walk (top, topdown = false ):

For name in files:

OS. Remove (OS. Path. Join (root, name ))

For name in dirs:

OS. rmdir (OS. Path. Join (root, name ))

New in Version 2.3.

Shutil. rmtree (path [, ignore_errors [, onerror])
Delete folder

If you have introduced so much, you only need to check the documents of the OS and shutil modules .. Note the following when writing shell scripts:

1. environment variables. The Python environment variables are stored in the OS. Environ dictionary. You can use the normal dictionary to modify them. When other programs are started using the system, the environment variables are automatically inherited. For example:
OS. Environ ["fish"] = "nothing"
Note that the value of the environment variable can only be a string. Unlike shell, Python does not have the export environment variable concept. Why not? Because Python does not need to have :-)

2. the OS. Path module contains many functions about path name processing. In Shell, path name processing is not very important, but it is often used in Python. The two most common examples are separation and merge directory names and file names:

OS. Path. Split (PATH)-> (dirname, basename)
This function separates a path into two parts, for example, OS. path. split ("/Foo/bar. dat) returns ("/foo", "bar. dat ")

OS. Path. Join (dirname, basename)
This function combines the directory name and file name into a complete path name, such as OS. path. join ("/Foo", "bar. dat) returns "/Foo/bar. dat ". This function is the opposite of OS. Path. Split.

There are also these functions:

OS. Path. commonprefix (list)
Returns the longest path of all paths in the list.
For example:
>>> OS. Path. commonprefix (['/home/TD', '/home/TD/ff','/home/TD/fff'])
'/Home/TD'

OS. Path. lexists (PATH)
The difference from OS. Path. exists (PATH) is that if a broken link exists, true is returned.

OS. Path. basename ('/Foo/bar. dat ')

>>> Bar. dat

OS. Path. dirname ('/Foo/bar. dat ')

>>>/Foo

OS. Path. realpath (PATH)
Returns the actual path of the path and removes the symbolic link.
OS. Path. relpath (path [, start])
Returns a "related path", the current directory, or an optional start
Return a relative filepath to path either from the current directory or from an optional start point.
For example:
>>> OS. Path. relpath ('/home/jimin','/usr/lib /')
'Http: // www.cnblogs.com/home/jimin'
OS. Path. samefile (path1, path2)
If path1 and path2 are the same files or directories, true is returned.
OS. Path. sameopenfile (FP1, fp2)
Returns true if FP1 and fp2 point to the same file.
OS. Path. samestat (STAT1, stat2)
If stat tuple STAT1 and stat2 point to the same file, true is returned. The stat tuple structure is generated by fstat (), lstat (), Stat ()

OS. Path. abspath (PATH)
Converts a path to an absolute path, which is equivalent to normpath (join (OS. getcwd (), PATH ))

OS. Path. expanduser (PATH)
Include "~" In Path "~" And "~ Convert user to user directory

OS. Path. expandvars (PATH)
Accept the extension of Environment Change. environment variables can be used in path.
For example:
>>> OS. Path. expandvars ('$ path ')
'/Usr/lib64/qt-3.3/bin:/usr/Kerberos/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin: /usr/local        
/Sbin:/usr/sbin:/home/Jimin/bin'
>>> OS. Path. expandvars ('$ home ')
'/Home/jimin'

OS. Path. expandvars (PATH)
Replace the "$ name" and "$ {name}" contained in the path based on the value of the environment variable, such as the Environment Variable fish = nothing, that OS. path. expandvars ("$ fish/ABC") returns "Nothing/ABC"

OS. Path. normpath (PATH)
Remove the "." and "." contained in the path ".."

OS. Path. splitext (PATH)
Separate the path into the basic name and extension. For example, OS. Path. splitext ("/Foo/bar.tar.bz2") returns ('/Foo/bar.tar', '.bz2 ′). Note the difference between it and OS. Path. Split ().

3. in the OS module, there is a very useful function called OS. stat () is not introduced because OS. the path module contains a group of functions that have the same functions, but the name is easier to remember.

OS. Path. exists (PATH)
Determine whether a file or directory exists

OS. Path. isfile (PATH)
Determine whether the path points to a common file rather than a directory

OS. Path. isdir (PATH)
Determine whether the path points to a directory rather than a common file

OS. Path. islink (PATH)
Determine whether the path points to a symbolic link

OS. Path. ismount (PATH)
Determine whether the path points to a mount point)

OS. Path. getatime (PATH)
Returns the last access time of the file or directory pointed to by path.

OS. Path. getmtime (PATH)
Returns the last modification time of the file or directory pointed to by path.

OS. Path. getctime (PATH)
Returns the creation time of the file pointed to by path.

OS. Path. getsize (PATH)
Returns the size of the file pointed to by path.

4. OS, shutil, glob (name of the regular expression file), tempfile (temporary file), PWD (Operation/etc/passwd file) are often used to write shell scripts in Python ), GRP (Operation/etc/group file), commands (get the output of a command ). The previous two articles have basically been introduced. The next few articles are very simple. Just read the documents.

5. SYS. argv is a list that stores the command line parameters of the python program. Here, SYS. argv [0] is the name of the program.

You can't just say you don't want to practice it. Next we will write a simple script for copying files. My colleague who asked me to write a script two days ago had a directory of tens of thousands of files. He wanted to copy these files to other directories and could not directly copy the directory itself. He tried "CP src/* DEST/" and reported a command line too long error, asking me to write a script for him. Start with Python:

Import sys, OS. Path, shutil
For f in OS. listdir (SYS. argv [1]):
Shutil. Copy (OS. Path. Join (SYS. argv [1], F), SYS. argv [2])

Try the post in the linuxapp version-rename all the files in a folder to 10001 ~ 10999. You can write as follows:

Import OS. Path, sys
Dirname = SYS. argv [1]
I = 10001.
For f in OS. listdir (dirname ):
Src = OS. Path. Join (dirname, F)
If OS. Path. isdir (SRC ):
Continue
OS. Rename (SRC, STR (I ))
I + = 1

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.