Python learning-command line parameters and OS modules in python

Source: Internet
Author: User

Command line parameters in python

In python, the sys. argv attribute provides access to command line parameters. The command line parameter is a parameter other than the program name when calling a program.

Sys. argv is a list of command line parameters.

Len (sys. argv) is the number of command line parameters

The following is a simple example.

#! /Usr/bin/python # coding: utf-8import sys # Load sys this module. For I in range (len (sys. argv): print "The % d parameter is: % s" % (I, sys. argv [I]) print

Run the above script:

Python argv. py 1 2 3

The result is as follows:

The 0th parameters are argv. py.

The 1st parameters are: 1

The 2nd parameters are: 2

The 3rd parameters are: 3


From the above script running results, we can see that the first parameter is the script name itself, that is, the 0th parameter. And so on.

With this sys. argv parameter, we can pass some parameters we want to use to the script.


Another common module, OS. path, can perform path operations. It provides functions to manage and operate all parts of the file path, obtain information about the file or subdirectory, and query the file path.

The following lists some common functions in the OS module:

Function Name: role:


OS. mkfifo ('path/filename')/OS. mknod ('path/filename', mode, device) create named pipelines/Create File System nodes

OS. remove ('path/filename') delete an object

OS. rename ('path/filename1', 'path/filename2')/renames () rename the file

OS. stat ('path/filename ') returns the File Information

OS. symlink ('path/filename', 'path/ln_filename ') to create a symbolic link

OS. utime () Update Timestamp

OS. tmpfile () creates and opens a new temporary file 'W + B '.

OS. walk () generates all file names under a directory tree


Directory/folder


OS. chdir ()/fchdir () change the current working directory/change the current working directory through a file descriptor

Chroot () changes the root directory of the current process

Listdir () is used to list objects in a specified directory.

Getcwd ()/getcwdu () returns the same current working directory/function, but returns a Unicode object

Mkdir ()/makedirs () create directory/create multi-level directory

Rmdir ()/removedirs () delete directory/delete multi-level directory


Access/permission

Access () Check permission Mode

Chmod () Change permission Mode

Chown ()/lchown () changes ower and group ID/functions the same, but does not track links

Umask () sets the default permission Mode


File descriptor operations

Open () underlying operating system open uses the standard built-in open () function for files)

Read ()/write () reads/writes data based on the file descriptor

Dup ()/dup2 () copies the file description symbols/functions are the same, but is copied to a file descriptor device number.

Makedev () creates an original device number from the major and minor device numbers

Major ()/minor () Get the major/minor device number from the original device number


The following are some functions used to access the path name in the OS. path module.

Function Description

OS. path. basename () Remove the directory path and return the file name.

OS. path. dirname () remove the file name and return the directory path.

OS. path. join () combines the separated parts into a path name.

OS. path. split () returns dirname (), basename () tuples

OS. path. splitdrive () returns dirvename, pathname) tuples

OS. path. splitext () returns the filename, extension) tuples.

Information

Getatime () returns the last access time

Getctime () returns the File Creation Time

Getmtime () returns the last file modification time

Getsize () returns the object size in bytes)

Query

Exists () specifies whether the path file or directory exists

Isabs () specifies whether the path is an absolute path

Isdir () specifies whether the path exists and is a directory

Isfile () specifies whether the path exists and is a file

Islink () specifies whether the path exists and is a symbolic link.

Ismount () specifies whether the path exists and is a mount point

Whether two samefile () path names point to the same file


The above modules and functions are often used in python scripts. Record them here. ^-^



This article is from the "linux learning" blog, please be sure to keep this source http://zhou123.blog.51cto.com/4355617/1284377

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.