Python OS Module

Source: Internet
Author: User

The OS module in the python standard library contains common operating system functions. This module is especially important if you want your program to be unrelated to the platform. That is, it allows a program to run in Linux and Windows without any changes or any problems.

The following lists some useful parts in the OS module. Most of them are simple and clear.

OS. SEP can replace the path delimiter specified by the operating system.
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 in 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. mkdir () function is used to create a directory.
The OS. makedirs () function is used to create a directory. If the directory in this path does not exist, it is automatically created, which is equivalent to mkdir-P in Linux.

The OS. linesep string specifies 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.

The OS. Path. isfile () and OS. Path. isdir () functions verify whether the given path is a file or a directory.

The OS. Path. Existe () function is used to check whether the GIVEN PATH actually exists.

OS and OS. Path modules
OS. listdir (dirname): List directories and files under dirname
OS. getcwd (): Get the current working directory
OS. curdir: returned but the former directory ('.')
OS. chdir (dirname): change the working directory to dirname
OS. umask (mask): sets the current File Permission mask and returns the previous permission mask.
OS. setsid (): disconnects a terminal from a process that is independent of the terminal (does not respond to SIGINT, sighup, and so on.

OS. Path. isdir (name): determines whether the name is a directory. If the name is not a directory, false is returned.
OS. Path. isfile (name): determines whether the name is a file. If the name does not exist, false is returned.
OS. Path. exists (name): determines whether a file or directory name exists.
OS. Path. getsize (name): Get the file size. If the name is a directory, return 0l
OS. Path. abspath (name): Obtain the absolute path.
OS. Path. normpath (PATH): standard path string format
OS. path. split (name): Split the file name and directory (in fact, if you fully use the directory, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)
OS. Path. splitext (): separates file names and extensions
OS. Path. Join (path, name): Connection directory and file name or directory
OS. Path. basename (PATH): returns the file name.
OS. Path. dirname (PATH): Return file path

------------------------------------------------------------------------------

Write something to familiarize yourself

Code:

#! /Usr/bin/Python

Import OS

FADD = ("a.txt", "B .txt", "d/d.txt ")
Fdel = ("c.txt ",)

Inputpath = raw_input ()
If (OS. Path. isdir (inputpath) = false ):
Print "you have input a incrrect path"
Else:
OS. chdir (inputpath)
BF = OS. listdir (inputpath)
For f in FADD:
(Dr, FL) = OS. Path. Split (f)
NDR = OS. getcwd () + "\"
If (Dr! = ""):
NDR = NDR + DR + "\"
OS. mkdir (NDR)
OS. chdir (NDR)
HF = file (NDR + FL, 'w ')
HF. Close ()
OS. chdir (inputpath)
For f in fdel:
OS. Remove (f)
AF = OS. listdir (inputpath)

From: http://hi.baidu.com/happynp/blog/item/729243f902d5a751242df2c2.html

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.