Import Osos.name # Operating system name Os.environ #版本 # to get the value of an environment variable, you can call the Os.getenv () function os.getenv (' PATH ')
Manipulating Files and directories:
Part of the function of manipulating files and directories is placed in the os
module, part of the os.path
module
# View the absolute path of the current directory: Os.path.abspath ('. ') ' /users/michael ' # Create a new directory under a directory, # First, the full path of the new directory is represented: Os.path.join ('/users/michael ', ' testdir ') '/users/michael/testdir ' # then create a directory: Os.mkdir ('/users/michael/testdir ') # Remove a directory: Os.rmdir ('/users/michael/testdir ')
os.path.join()
Functions: Merging paths
os.path.splitext()
Allows you to get the file name extension directly
# Rename File: Os.rename (' test.txt ', ' test.py ') # Delete file: Os.remove (' test.py ')
shutil
module provides copyfile()
functions, you can also shutil
find a lot of practical functions in the module, they can be seen as a os
complement to the module
List all directories in the current directory
[x for X in Os.listdir ('. ') if Os.path.isdir (x)] ['. Lein ', '. Local ', '. M2 ', '. npm ', '. ssh ', '. Trash ', '. Vim ', ' adlm ', ' applications ', ' Desktop ', ...]
Lists the. py files under the current path
[x for X in Os.listdir ('. ') if Os.path.isfile (x) and Os.path.splitext (x) [1]== '. Py '] [' apis.py ', ' config.py ', ' models.py ', ' pymonitor.py ', ' test_db.py ', ' urls.py ', ' wsgiapp.py ']
Python Operations files and directories