Summary of common methods in Python standard library OS
- 2020-06-12 09:56:29
- OfStack
preface
We often deal with files and directories, for which python provides an os module, which contains many functions for manipulating files and directories. This os library is often used in writing some system scripts or automatic operation and maintenance scripts, so here is a tidy, convenient for you to find and learn when you need or need friends. Let's start with a detailed introduction.
1.
os.sep
Get the directory separator used by the current operating system, for example Windows will get \ and Linux/Unix will get /
os.name
For the operating system in use, Windows is the NT kernel, so you get nt, and Linux/Unix users get posix
2,
os.getcwd()
Gets the current working directory, which is the directory path where the Python script is currently working.
3,
os.getenv()
Used to obtain environment variables
os.getenv('PATH')
4,
os.environ
You can get and modify environment variables
print(os.environ['PATH'])
os.environ += 'D:/testdir/bin/'
print(os.environ["PATH"])
5,
os.listdir()
Lists all directories and files in a directory
print(os.listdir())
6,
os.remove()
Delete the file
os.remove('D:/test.file')
7,
os.system()
Run the Shell or CMD commands
os.system('ifconfig')
Eight,
os.linesep
Gets the line terminator used by the current platform. For example, Windows USES \r\n, Linux USES \n and Mac USES \r.
9,
os.path.split()
Get a list where list[0] is the path and list[1] is the filename
path = 'D:/game/gtav/bin/gtav.exe'
print(os.path.split(path)[0])
print(os.path.split(path)[1])
10,
os.name
0
and
os.path.isdir()
Determine if the path is a file/directory
print(os.path.isfile('D:/game/gtav/bin/gtav.exe'))
print(os.path.isdir('D:/game/gtav/bin'))
11,
os.path.existe()
The function is used to verify that the given path actually exists
path = 'D:\\hadoop-2.6.5\\bin'
print(os.path.exists(path))
path = 'D:\\hadoop-2.6.5\\bin\\hadoop'
print(os.path.exists(path))
12,
os.chdir(dirname)
Toggle the working directory, equivalent to the cd command
os.chdir('D:/game/gtav/')
print(os.getcwd())
13,
os.path.getsize(name)
Gets the file size, in bytes
size = os.path.getsize('D:/iso/debian-8.6.0-amd64-DVD-1.iso')
print(size/1024/1024/1024, 'GB')
14,
os.path.abspath(name)
Get the absolute path. If I have 1 file file.txt in the Python working directory, Then I can either directly open(' file.txt ') or get the absolute path print(' os.path.txt '). It can also be used to normalize the path string print(os.path.abspath ('D:/game\gtav/bin/ gtav.exe '))
15,
os.path.normpath(path)
This is used to specify paths
print(os.environ['PATH'])
os.environ += 'D:/testdir/bin/'
print(os.environ["PATH"])
0
16,
os.path.splitext()
Gets the file name and extension
print(os.environ['PATH'])
os.environ += 'D:/testdir/bin/'
print(os.environ["PATH"])
1
17,
os.path.join(path,name)
Connect directories and filenames without adding their own delimiters, which can reduce bug rate and improve cross-platform performance
18,
os.path.basename(path)
Gets the file name in the path
19,
os.getcwd()
0
Gets the directory name in the path
conclusion