Python OS Module

Source: Internet
Author: User

OS Module Common Commands

python The standard library for os modules contain common operating system features. 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, and without any problems, in the linux windows run.
Below are some of the os The more useful part of the module. Most of them are simple and straightforward.

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

OS.GETCWD () function to get the current working directory, which is the current Python the directory path where the script works.

os.getenv () and the os.putenv () The function is used to read and set environment variables respectively.

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

>>> Os.listdir (OS.GETCWD ())
[' Django ', ' DLLs ', ' Doc ', ' include ', ' Lib ', ' Libs ', ' LICENSE.txt ',' mysql-python-wininst.log ', ' NEWS.txt ', ' Pil-wininst.log ', "Python.exe ', ' pythonw.exe ', ' README.txt ', ' Removemysql-python.exe ', ' RemovePIL.exe ', ' Removesetuptools.exe ', ' Scripts ', ' setuptools-wininst.log ', ' tcl ', ' Tools ', ' W9xpopen.exe ']

os.remove () The function is used to delete a file.

Os.system () function is used to run shell command, equivalent to windows cmd The command entered in the window.
If it is system words are linux Span style= "font-family:arial" >
> >> os.system (' dir ')
0
> >> os.system (' cmd ') # start dos

Os.sep returns a delimiter that can replace an operating system-specific path divider.

the os.linesep string gives the line terminator used by the current platform. For example,Windows uses ' \ r \ n ',Linux uses ' \ n ' and Mac Use ' \ r ' .
>>> os.linesep
' \ r \ n ' #Windows use ' \ r \ n',Linux uses ' \ n ' and Mac Use ' \ r ' .
>>> Os.sep
' \ \ ' #Windows

Os.listdir (dirname) : Lists dirname directories and files under the

os.curdir: Returns the current directory ---('. ')

Os.chdir (dirname): Changing the working directory to dirname

Python OS Module

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.