SYS and OS modules

Source: Internet
Author: User
Tags exit in

http://blog.csdn.net/pipisorry/article/details/42167683

Python's system modules include: SYS, OS, glob, Socket, threading, _thread, queue, time, Timeit, subprocess, multiprocessing, signal, select, Shutil, Tempfile and so on.

Most system-level interfaces focus on: SYS and OS two modules.

The SYS module contains:
Platform and version information, such as Sys.platform, Sys.maxsize, sys.version
Module Search Path Sys.path
Module table Sys.modules, which is a dictionary containing the Name:module information for the import module in the Python program
Exception information, such as Sys.exc_info ()
Command-line Arguments SYS.ARGV
Standard flow, such as Sys.stdin, Sys.stdout, Sys.stderr
Program exits call Sys.exit

The OS module contains
Tasks Tools
Shell variables Os.environ
Running programs Os.system, Os.popen, OS.EXECV, OS.SPAWNV
Spawning processes os.fork, Os.pipe, Os.waitpid, Os.kill
Descriptor files, locks Os.open, Os.read, Os.write
File processing Os.remove, Os.rename, Os.mkfifo, Os.mkdir, Os.rmdir
Administrative Tools OS.GETCWD, Os.chdir, Os.chmod, Os.getpid, Os.listdir, os.access
Portability Tools Os.sep, Os.pathsep, Os.curdir, Os.path.split, Os.path.join
Pathname Tools os.path.exists (' path '), Os.path.isdir (' path '), Os.path.getsize (' path ')


Os

The module contains common operating system features. This module can be used to write platform-independent programs, such as using OS.SEP to replace operating system-specific path separators.

Common methods:

Os.name: Gets the current system platform, Windows returns ' NT ', and Linux returns ' POSIX '.

OS.LINESEP: Gets the line terminator used by the current platform. Windows returns '/r/n ', Linux uses '/n '.

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

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

The Os.remove (path/filename) function is used to delete a file.

The Os.system () function is used to run the shell command. This command makes it easy to invoke or execute other scripts and commands

 
   
  
  1. >>>os.system (' notepad ')
  2. >>>os.system (' notepad *.txt ')

The Os.path.split () function returns the directory name and file name of a path.

 
   
  
  1. >>> os.path.split ('/home/shirley/myself/code/icbc.txt ')
  2. ('/home/shirley/myself/code ', ' icbc.txt ')

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

The Os.path.existe () function is used to verify that the given path really exists.


Sys

SYS module can be see Python document http://docs.python.org/library/sys.html.

Common functions:

SYS.ARGV: Implements passing parameters from outside the program to the program.

 
   
  
  1. Import SYS
  2. Print sys.argv[0]
  3. Print sys.argv[1]
  4. Print sys.argv[2
 
   
  
  1. >>>python print.py arg1 arg2

In general, Argv[0] represents the file name of the program being executed.

Sys.exit ([arg]): Exit in the middle of the program, arg=0 for normal exit.

Sys.getdefaultencoding (): Gets the system current encoding, which is generally ASCII by default.

Sys.setdefaultencoding (): Set system default encoding, do not see this method when executing dir (SYS), do not pass in interpreter, can execute reload (SYS), execute setdefaultencoding (' UTF8 ' ), the system default encoding is set to UTF8. (See Set system default encoding)

Sys.getfilesystemencoding (): Gets the file system using encoding, returns ' MBCS ' under Windows, and returns ' Utf-8 ' under Mac.

Sys.path: Gets a collection of strings for the specified module search path, which can be placed in a given path, and can be found correctly when import is in the program.

Sys.platform: Gets the current system platform.


Get the location of the directory where the Python script resides

1. Previous methods
If you want to get the current directory where the program is running, you can use the OS.GETCWD () function of the OS module.

If you want to get the directory location of the currently executing script, you need to use the SYS module's sys.path[0] variable or sys.argv[0] to get it. The fact that Sys.path is Python will look for a list of search paths for modules, sys.path[0] and sys.argv[0] is one thing because Python automatically adds sys.argv[0] to Sys.path.

Specifically, if you execute Python getpath\getpath.py in the C:\test directory, then OS.GETCWD () outputs "C:\test" and sys.path[0] outputs "C:\test\getpath".

More specifically, if you compile the Python script as an executable using the Py2exe module, the output of sys.path[0] will change:
If the dependent library is packaged as a zip file by default, then Sys.path[0] will output "C:\test\getpath\libarary.zip";
If the Zipfile=none parameter is specified in the setup.py, the dependent library will be packaged into the exe file, then sys.path[0] will output "C:\test\getpath\getpath.exe".

2. The right approach
But these are not actually the location of the directory where the script files are located.
For example, there is a directory named sub under the C:\test directory, C:\test directory under the Getpath.py,sub directory has sub_path.py,getpath.py call sub_path.py; we are in C: \ Test to execute the getpath.py. If we use sys.path[0 in sub_path.py, we actually get the directory path "C:\test" where the getpath.py is located, because the Python virtual machine is executed from the beginning of the getpath.py. If you want to get the path to the sub_path.py, you get this:
Os.path.split (Os.path.realpath (__file__)) [0]
Although __file__ is the full path of the. py file, this variable sometimes returns a relative path, sometimes an absolute path, so it is also handled with the Os.path.realpath () function. In this case, the Os.path.realpath (__file__) output is "C:\test\sub\sub_path.py", while Os.path.split (Os.path.realpath (__file__)) [0] The output is "C:\test\sub".

3. Example Description
The difference between OS.GETCWD (), sys.path[0] (sys.argv[0]) and __file__ is this:
Assume that the directory structure is:
C:\test
|
[Dir] GetPath
|
[File] path.py
[Dir] Sub
|
[File] sub_path.py

Then we execute the python getpath/path.py below the C:\test, at which point the values corresponding to each usage in sub_path.py are actually:
OS.GETCWD () "C:\test", which takes the starting execution directory
Sys.path[0] or sys.argv[0] "C:\test\getpath", which is the directory where the script was executed initially
Os.path.split (Os.path.realpath (__file__)) [0] "C:\test\getpath\sub", which is the directory where __file__ files sub_path.py

"How to get the location of the directory where the Python script resides"


from:http://blog.csdn.net/pipisorry/article/details/42167683


SYS and OS modules

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.