Detailed explanation of the OS module in python and the pythonos Module

Source: Internet
Author: User

Detailed explanation of the OS module in python and the pythonos Module

The OS module allows you to create, delete, and view file attributes for directories or files, as well as perform path operations on files and directories. For example: absolute path, parent directory ......

os.sepIt can replace the path separator specified by the operating system. "\" In windows and "/" in Linux "/"

os.linesepString indicates 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 '.

os.pathsepOutput the string used to split the file PATH. The system uses this character to split the search PATH (like PATH), for example, ':' on POSIX, ';' on Windows ';'

os.getcwd() Obtain the current working directory, that is, the directory path of the current python script.

os.chdir("dirname") Change the working directory of the current script; equivalent to cd under shell

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

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

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

OS. makedirs ('dirname1/dirname2 ') can generate multi-layer recursive directories.

os.remove(file) Delete an object

os.removedirs('dirname1') If the directory is empty, delete it and recursively go to the upper-level directory. If the directory is empty, delete it.

os.rmdir('dirname') Delete a single-stage empty directory. If the directory is not empty, it cannot be deleted. An error is returned, which is equivalent to rmdir dirname in shell.

os.listdir('dirname') List all files and subdirectories in a specified directory, including hidden files, and print them in list mode.

os.rename("oldname","newname") Rename the file/directory. If newname exists, an error occurs when replacing it.

os.replace(src,dest) Rename a file/directory. If dest indicates a file, the existing file overwrites the original one. No error is reported. If the directory exists, an error is returned.

OS. chmod (path, mode, *, dir_fd = None, follow_symlinks = True)

Ex: OS. chmod ('C: \ my_cmd_file \ test. Sh', 755)

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

os.utime(path,times) The modified time attribute times is a tuples (atime, mtime), which can be obtained through OS. stat.

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

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

2. The default value of topdown is "True", indicating that the files under the directory tree are returned first, and then when the subdirectory of the directory tree is traversed. The value of Topdown is "False,

Then, the system first traverses the subdirectories of the directory tree, returns the files under the subdirectories, and finally returns the files under the root directory.

3. the default value of onerror is "None", indicating to ignore Errors generated by file time. if it is not empty, a user-defined function is provided to indicate an error message and continue traversal or throw an exception to stop traversal.

This function returns a tuple with three elements representing the directory currently traversed, the directory list currently traversed, and the file list of the directory currently traversed.

os.walk()Example:

>>> Import OS >>> for root, dirs, files in OS. walk ("wd/chat", topdown = False ):... for name in files :... print (OS. path. join (root, name) # print the absolute file path... for name in dirs :... print (OS. path. join (root, name) # print the absolute directory path

os.nameString indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix '.

os.getenv()Obtain an environment variable. If none is not returned

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

os.environ[]Get the value of the environment variable. The two values are equivalent: OS. environ ['home'] <-> OS. getenv ('home ')

os.system(command)Function is used to run shell commands.

os.popen("bash command")Run the shell command to generate an object, which can be assigned to a variable and read by 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.