We often deal with files and directories, and for these operations Python provides an OS module that contains many functions for manipulating files and directories. To know what the methods are, we can run the following statement to output all of the OS module information on the screen:
Import oshelp (OS)
If you understand Linux basic operations, some of the following OS methods should be familiar, because the basic and Linux operating methods are the same. Here are a few common:
1.OS.GETCWD () Gets the current path
A few ways to talk about paths, by the way
Import osBase1 = Os.path.dirname (__file__) Base2 = Os.path.abspath (__file__) Base_dir1 = Os.path.dirname ( Os.path.dirname (__file__)) Base_dir2 = Os.path.dirname (Os.path.abspath (__file__)) Join_dir = Os.path.join (Base_dir1, ' Templates ') Join_dir2 = Os.path.join (Base_dir1, ' ... ') Print "__file__: ", __file__print "Os.path.dirname: ", Base1print "Os.path.abspath: ", Base2print "double DirName: ", Base_dir1print" DirName and Abspath: ", Base_dir2print" Join_dir: ", join_dir#__file__: /users /admin/pycharmprojects/test/dirtest.py#os.path.dirname: /users/admin/pycharmprojects/test#os.path.abspath: /users/admin/pycharmprojects/test/dirtest.py#double dirname: /users/admin/pycharmprojects#dirname and Abspath: /users/admin/pycharmprojects/test#join_dir: /users/admin/pycharmprojects/templates
2.os.listdir (path) to get the contents of the directory
3.os.mkdir (path) Create directory
4.os.rmdir (path) Delete directory
5.os.isdir (path) os.isfile (path) determines whether it is a directory or a file
6.os.remove (path) Delete file
7.os.rename (old, new) rename file or directory
The 8.os.name output string indicates the platform being used. If the window is ' NT ', it is ' POSIX ' for Linux/unix users
9.os.system () Run shell command
10.os.path.split () returns the directory name and file name of a path
11.os.path.splitext () detach file name and extension
12.os.path.getsize (name) Gets the file size if name is directory returned 0L
13.os.getegid () returns the valid group ID that the current process belongs to Only UNIX is available
14.os.geteuid () returns the user ID to which the current process belongs (Unix)
15.os.getgid () returns the real group ID to which the current process belongs (Real team ID)
16.os.getlogin () returns the current login user name
17.OS.GETPGRP () Returns the ID of the current process group (Unix)
18.os.getpid () Returns the PID of the current process (Unix, Windows)
19.os.getppid () Returns the ID of the current process parent process (Unix)
20.os.getuid () Returns the user ID (Unix) to which the current process belongs
Python third-party library series Nine--os Library