Detailed description of the OS module in Python

Source: Internet
Author: User
The OS module provides new/delete/view file properties for directories or files, as well as path operations for files and directories. For example: absolute path, parent directory ...

os.sepCan replace the operating system-specific path delimiter. "\ \" under Windows, "/" under Linux

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.pathsepOutput the string used to split the file path, which the system uses to split the search path (like path), such as POSIX on ': ', on 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, equivalent to the shell CD

os.curdir Return to current directory: ('. ')

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

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

Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories

os.remove(file) Delete a file

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

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

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 if newname exists replace error

os.replace(src,dest) Rename the file/directory, if the dest is a file, there is overwrite the original file, no error, if the directory, there will be an error

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 attribute The Times is a tuple, (atime,mtime), and these two times 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 files under the directory tree are returned first, and then the subdirectories of the directory tree are traversed. When the value of Topdown is "False",

This means traversing the directory tree's subdirectories, returning the files under subdirectories, and finally returning the files under the root directory.

The default value of 3.onerror is "None", which means that errors resulting from file traversal are ignored. If not NULL, provide a custom function tip error message after you continue traversing or throwing an exception to abort the traversal

The function returns a tuple that has 3 elements that represent the currently traversed directory, the list of directories currently traversed, and the list of files for the currently traversed directory

os.walk()Example:

>>> import os>>> for root, dirs, files in Os.walk ("Wd/chat", Topdown=false): .... For name in Files: ... pr Int (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's ' 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[]Get the value of the environment variable equivalent: os.environ[' home '] <->os.getenv (' home ')

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

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

  • 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.