Concise python tutorial ten----python standard library

Source: Internet
Author: User
Tags readfile

Import sysdef ReadFile (filename):'Print A file to the standard output.'F=file (filename) whileTrue:line=F.readline ()ifLen (line) = =0:             BreakPrint Line, F.close ()ifLen (SYS.ARGV) <2: Print'No action specified.'sys.exit ()ifsys.argv[1].startswith ('--'): Option= sys.argv[1][2:]    ifoption = ='version': Print'Version 1.2'elif Option==' Help': Print" "\prints files to the standard output.    Any number of the files can be specified. Option include:--version:prints the version number--help:display ThisHelp" "    Else:         forFileNameinchsys.argv[1:]: ReadFile (filename)

Results:

 $ python cat.pyno action specified.$ python cat.py --helpthis program prints files to the standard output. Any number of the files can be specified. Options include: --version:prints the version number --help:display this   help$ python cat.py --versionversion  1.2  $ python cat.py --nonsenseunknown option.$ python cat.py poem.txtprogramming  is   Funwhen the work  is   done  if   Wanna make your work also fun:use Python ! 

When the Python program is running, not in interactive mode, there is always at least one item in the SYS.ARGV list. It is the name of the program that is currently running as sys.argv[0].

SYS module

>>>Import sys>>> sys.version

The sys.version string gives you the version information for the Python installation. The Sys.version_info tuple provides an easier way for your program to have the Python version requirements feature.

Sys.stdin, Sys.stdout, sys.stderr they correspond to your program's standard input, standard output, and standard error streams, respectively.

OS Module

This module contains common operating system functions. This module is especially important if you want your program to be platform-agnostic.

That is, it allows a program to be written without any changes or problems, and can be run under Linux and widows.

OS.SEP: Can replace the operating system-specific path delimiter.

The Os.name string indicates the platform you are using. For example, Windows is ' NT ', Linux/unix user, it's ' POSIX '

OS.GETCWD () function: Gets the current working directory, which is the directory path of the current Python script work.

Os.getenv () and Os.putenv () functions: Read and set environment variables.

Os.listdir (): Returns all file and directory names under the specified directory

Os.remove () function: Delete a file

Os.system () function: Run shell command

The OS.LINESEP string gives the line terminator used by the current platform. Windows uses ' \ r \ n ', Linux uses ' \ n ', and Mac uses ' \ R '.

Os.path.split () Function: Returns the directory name and file name of a path

The Os.path.isfile () and Os.path.isdir () functions respectively verify that the given path is a file or a directory.

Os.path.exists () function: verifies that the given path is actually present.

Concise python tutorial ten----python standard library

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.