python1.1-----OS Module/os.path module

Source: Internet
Author: User

OS module : Contains the functionality of a common operating system.

import OS introduced into the operating system module.
Os.name Gets the operating system type (nt->windows posix->linux,unix,mac os X).
os.uname details of the print operating system (not supported by Windows).
Os.environ gets all environment variables for the operating system. For example, when you enter a program in CMD, the system will look in the list of environment variables to find out which path contains the program. Found on the run, can not find the prompt error.
os.environ.get (str) Gets the environment variable for the specified str.
Os.curdir () gets the current directory./a/.
OS.GETCWD () Gets the current directory, which is the directory where the current script resides.
os.listdir (path) returns all the files in the specified directory in the form of a list.
os.mkdir (directory name) creates a new directory (new folder) under the current directory.
or Os.mkdir (absolute path/directory name) creates a new directory (new folder) under the absolute path.
Os.rmdir (absolute path/directory name) deletes the specified directory (folder) under the absolute path.
os.stat (filename) Gets the properties of the file.
Os.rename (oldname,newname) replaces the file or directory in the current directory with newname instead of oldname.
os.remove (filename) deletes the normal file under the current directory.
Os.system () runs the shell command.
For example:
os.system (' Notepad ') starts Notepad.
Os.system (' write ') starts WordPad.
os.system (' MSPaint ') to open the drawing.
os.system (' msconfig ') opens the system settings.
Os.system (' shutdon-s-T 1 ') automatically shuts down within 1 seconds.
Os.system ('-a ') cancels the automatic shutdown.
os.system (' taskkill/f/im notepad.exe ')   Close the Notepad program.


#有些方法存在os模块里, some exist in os.path module .
import os.path   Introduce the Os.path module.
os.path.abspath ('. ')   Gets the current absolute path.
os.path.join (path,dirname)   Merge paths.
example: P1=r ' C:\Users\xlg\ Desktop\project '      p2=r ' idea\a\new.txt ' #p2开头不能有 ' \ '
Os.path.join (P1,P2)  -get > ' C : \users\xlg\desktop\project\idea\a\new.txt '.

os.path.split (path): Splits path into a tuple, The second element is the last file or directory name.
example: p1= ' C:\Users\xlg\Desktop \project\.idea\a\new.txt '.
os.path.split (p1)-Get > (' C:\Users\xlg\Desktop\Project\ . Idea\a ', ' new.txt ')

Os.path.splitext (path): Divides path into a tuple, the second tuple is an extension, and if path does not contain an extension, the tuple's second string length is 0
Os.path.isdir (PATH): Determines if path is a directory and returns true if path is a directory, otherwise false
Os.path.isfile (PATH): Determine if there is a file on path, if the file exists, returns true, otherwise false
Os.path.exists (PATH): Determines if a path is present on the computer, returns True if present, otherwise false.
Os.path.getsize (PATH): Gets the number of bytes of the file.
Os.path.dirname (PATH): Gets the file name.

python1.1-----OS Module/os.path 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.