The OS module in Python is detailed _python

Source: Internet
Author: User
Tags chmod mkdir parent directory posix python script in python

The OS module provides new/delete/view file properties for directories or files, and also provides path actions for files and directories. For example: absolute path, parent directory ...

os.sepCan replace the operating system-specific path separator. Under Windows "\", Linux is "/"

os.linesepThe string gives the line terminator used by the current platform. For example, Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '.

os.pathsepOutputs a string that is used to segment the path of a file, using this character to split the search path (like path), such as on POSIX ': ', Windows ';

os.getcwd() Gets the current working directory, which is the directory path of the current Python script work

os.chdir("dirname") Change the current script working directory; the equivalent of a shell CD

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

os.pardir Gets the parent directory string name of the current directory: ('.. ')

os.mkdir('dirname') Generate a single level directory; equivalent to mkdir dirname in the shell

Os.makedirs (' dirname1/dirname2 ') generates a multiple-layer recursive directory

os.remove(file) Delete a file

os.removedirs('dirname1') If the directory is empty, delete, and then recursively go to the previous directory, if it is empty, then delete, and so on

os.rmdir('dirname') Delete a single level empty directory, if the directory is not empty, can not be deleted, error; the equivalent of RmDir dirname in the shell

os.listdir('dirname') Lists all files and subdirectories under the specified directory, including hidden files, and prints as a list

os.rename("oldname","newname") Rename file/directory, replace error if newname exists

os.replace(src,dest) Renames the file/directory, if Dest is the file, the existence overwrites the original file, does not complain;

Os.chmod (path, mode, *, Dir_fd=none, follow_symlinks=true)

Ex:os.chmod (' c:\\my_share_file\\test.sh ', 755)

os.stat('path/filename') Get File/directory information

os.utime(path,times) Modify Time Property The Times is a tuple, (atime,mtime), which can be obtained by os.stat

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

1.top indicates the path of the directory tree that needs to be traversed

The default value of 2.topdown is "True", which means that the file in the directory tree is returned first, and then the subdirectory of the directory tree is traversed. When the value of Topdown is ' False ',

This means that you first traverse the subdirectory of the directory tree, return the files under the subdirectory, and finally return the files in the root directory.

The default value of 3.onerror is "None", which indicates that errors generated when the file is traversed are ignored. If not NULL, provide a custom function to prompt the error message to continue traversing or throw an exception abort traversal

The function returns a tuple that has 3 elements, each representing the currently traversed directory, the currently traversed directory list, and the file list of the currently traversed directory

os.walk()Example:

>>> import os

>>> for root, dirs, files in Os.walk ("Wd/chat", Topdown=false):

... for name in fi Les:.

print (Os.path.join (root, name)) #打印文件绝对路径 ... for

name in dirs:

... print (Os.path.join (root, name) ) #打印目录绝对路径

os.nameThe string indicates the platform you are using. For example, for Windows, it is ' NT ', and for Linux/unix users, it's ' POSIX '.

os.getenv()Gets an environment variable, if none is returned

os.putenv(key, value)Set an environment variable value

os.environ[]Getting the value of an environment variable is equivalent: os.environ[' home '] <->os.getenv (' home ')

os.system(command)function to run the shell command.

os.popen("bash command")Run the shell command, build the object, assign it to the variable, and read it with read

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.