Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Os module of python standard library module

Shulou Source: shulou.com Published: 2022-06-03 07:07:01 09月28日 Update

Os module

Action

Call system module

Common usage

Os.getcwd () gets the current working directory, that is, the directory path where the current python script works

Os.chdir ("dirname") changes the current script working directory; it is equivalent to cd under shell

Os.curdir returns the current directory: ('.')

Os.makedirs ('dirname1/dirname2') creates a multi-tier directory

Delete if the os.removedirs ('dirname1') directory is empty

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

Os.rmdir ('dirname') deletes a single-level empty directory. If the directory is not empty, it cannot be deleted and an error is reported. It is equivalent to rmdir dirname in shell.

Os.listdir ('dirname') lists all files and subdirectories in the specified directory, including hidden files, and prints them as a list

Os.remove () deletes a file

Os.rename ("oldname", "newname") file rename

Os.stat ('path/filename') gets the directory information of the file

Os.sep outputs operating system-specific path delimiters,\ in win and / in Linux

Os.linesep outputs the line Terminator used by the current platform,\ t\ n under win\ n under Linux\ n

Os.pathsep outputs the string used to split the file path

The os.name output string indicates the current use of the platform. Win- > nt; Linux- > posix

Os.system ("bash command") runs the shell command

Os.environ gets the environment variables of the current system

Os.path.abspath (path) returns the absolute path of path normalization

Os.path.split (path) splits path into directories and file tuples to return

Os.path.dirname (path) returns the directory of path. It's actually the first element of os.path.split (path).

Os.path.basename (path) returns the last file name of the path. If the path ends with / or\, a null value is returned. The second element of os.path.split (path)

Os.path.exists (path) determines whether the path path exists. If it exists, it is true, otherwise it is false.

Os.path.isabs (path) returns True if path is an absolute path

Os.path.isfile (path) returns True if path is an existing file. Otherwise, return False

Os.path.isdir (path) returns True if path is an existing directory. Otherwise, return False

Os.path.join (path2 [, path3 [,...]]) Combine multiple paths together and return

Os.path.getatime (path) returns the last access time of the file or directory pointed to by path

Os.path.getmtime (path) returns the last modification time of the file or directory pointed to by path

Display and demonstration

Get the current working directory

Import os

Os.getcwd ()

'/ home/tomcat'

Switch work path

Os.chdir ('python_test')

Os.getcwd ()

'/ home/tomcat/python_test'

Create a multi-tier directory

Os.makedirs ('a _ 1max _ a2')

Delete multi-tier directory

Os.removedirs ('a1')

Error

Traceback (most recent call last):

File "", line 1, in

File "/ usr/python/lib/python3.6/os.py", line 259, in removedirs

Rmdir (name)

OSError: [Errno 39] Directory not empty:'A1'

Os.removedirs ('a _ 1max _ a2')

Create a single-layer directory

Os.mkdir ('a1')

Os.chdir ('a1')

Os.getcwd ()

'/ home/tomcat/python_test/a1'

Delete a single-layer directory

Os.rmdir ('a1')

Show the contents of the current directory

Os.listdir (os.getcwd ())

['server5.py',' cleint5-1.pyrogen, 'server3.py',' client3-1.pyrogen, 'logs',' scp_dir.py']

Determine whether the file exists or not

Os.path.exists ('/ home/tomcat/python_test/12.txt')

False

Os.path.isfile ('/ home/tomcat/python_test/10.txt')

True

View the platform of the current operating system

Os.name

'posix'

Get the file name

Os.path.basename ('/ home/tomcat/python_test/10.txt')

'10.txt'

Combine multiple paths and return

Os.path.join (os.getcwd (), 'test1')

'/ home/tomcat/python_test/test1'

Tags: Directory file path work system output multi-tier platform module operating system element single-tier single-level multiple character string point time script function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft MySQL Huawei Shulou Tech Info