Python's OS module is a module that is closely integrated with the operating system, which provides many more useful methods.
-
OS.GETCWD () Gets the current working directory, which is the directory where the Python script runs
Print (OS.GETCWD ())
Results:/users/ssspure/pycharmprojects/file/osmodule
2. Os.chdir ("dirname") switch to the specified directory, the return value is None
3. Os.curdir This is not a function, it returns a constant representing the current directory
The return value is: "."
Official explanation: The constant string used by the operating system to refer to the current directory. This is '. ' For Windows and POSIX. Also available via Os.path.
4. Os.pardir is the same as the above Os.curdir, the return value is: "..."
Official explanation: The constant string used by the operating system to refer to the parent directory. This is ': ' For Windows and POSIX. Also available via Os.path.
5. Os.makedirs (DirName, mode=0o777, exist_ok=false) Create a directory where you can specify multiple directories that do not exist, and Python recursively create those directories
Os.makedirs ("Test/demo", 0o700)
For example, in the preceding code, the test directory is created in the current directory, and then the demo directory is created under the test directory.
The mode parameter refers to the ability to set the creation of a directory, one thing to note is that the first bit of permissions must be 7, or the creation of multiple directories will be an error, as shown in the following code.
Os.makedirs ("Test/demo", 0o600, True)
The above code can only create the test directory, and this error will occur: Permissionerror: [Errno] Permission denied: ' Test/demo '
This is because the test directory permission created is RW-------, which means that the current user does not have permission to enter the test directory, so the demo directory will be created with errors.
EXIST_OK: If this parameter is false, if the directory that you already want to create already exists, you will get an error, and if true, you will not get an error when the directory you are creating exists.
6. Os.removedirs ("Dir1/dir2/dir3") recursively delete directory
First delete the dir3, then delete the Dir2, and finally delete the Dir1. If there are other directories or files under the Dir2 directory, except Dir1, and then delete dir3, Dir2 will not be deleted.
7. Os.mkdir ("Test", 0o777)
Create a single empty directory
8. RmDir ("test") delete a single empty directory, if the directory is not empty the case will be error
9. Os.listdir (dirname) lists all files and folders under the specified directory, error hidden directory, return value is a list
Print (Os.listdir ("."))
The execution result of the above code is: [' __init__.py ', ' odmodule.py ', ' Test ']
Os.remove (file) Delete a file
Os.rename (File1, file2) Change the file1 file name to File2
Os.stat (file) gets information about file files that are encapsulated in a single tuple
Print (Os.stat ("Demo.txt"))
Results returned: Os.stat_result (st_mode=33188, st_ino=4067145, st_dev=16777220, St_nlink=1, st_uid=501, st_gid=20, St_size=8, St_ atime=1484573900, st_mtime=1484573764, st_ctime=1484573764)
OS.SEP returns the path delimiter under the current operating system
The path delimiter for Linux is "/" and Windows is "\ \"
OS.PATHSEP returns the environment variable delimiter for the current operating system
The path delimiter for Linux is ":", Windows is ";"
OS.LINESEP returns the line delimiter for the current operating system
The path delimiter for Linux is "\ n" and Windows is "\ r \ n"
This article is from the "ssspure" blog, make sure to keep this source http://ssspure.blog.51cto.com/8624394/1892409
Python OS Module