Import module:
Import OS
Display operating system type:
os.name
Posix
Show System Details:
os.uname ()
Posix.uname_result (sysname= ' Linux ', nodename= ' 18205c3de5e0 ', release= ' 3.13.0-32-generic ', version= ' #57-ubuntu SMP Tue Jul 03:51:08 UTC ', machine= ' x86_64 ')
To display environment variables:
os.environ
Environ ({' versioner_python_prefer_32_bit ': ' No ', ' term_program_version ': ' 326 ', ' LOGNAME ': ' Michael ', ' USER ': ' Michael ', ' PATH ': '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/x11/bin:/usr/local/mysql/bin ', ...})
Gets the value of an environment variable:
os.environ.get (' PATH ')
'/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/x11/bin:/usr/local/mysql/bin '
To view the absolute path of the current directory:
os.path.abspath ('. ')
/
To integrate the directory path, just take the path and look at it without actually creating it:
os.path.join ('/home/q/documents/temp/io ', ' NW ')
[Email protected]:~/documents/temp/io$ python join.py/home/s/documents/temp/io/nw[email protected]:~/documents/ temp/io$ lsa.txt dir.py file.py join.py stringio.py
To actually create a directory:
os.mkdir ('/home/q/documents/temp/io/nw ')
[Email protected]:~/documents/temp/io$ python join.py none[email protected]:~/documents/temp/io$ lsa.txt dir.py file.py join.py NW stringio.py
To delete a directory:
os.rmdir ('/home/q/documents/temp/io/nw ')
[Email protected]:~/documents/temp/io$ python join.py none[email protected]:~/documents/temp/io$ lsa.txt dir.py file.py join.py stringio.py
1. Use the OS module to write a program that implements the DIR-L output.
#!/usr/bin/python# -*- coding: utf-8 -*-from datetime import datetimeimport ospwd = os.path.abspath ('. ') Print (' Size Last Modified Name ') print ('------------------------------------------------------------') For f in os.listdir ( PWD): fsize = os.path.getsize (f) mtime = Datetime.fromtimestamp (Os.path.getmtime (f)). Strftime ('%y-%m-%d %h:%m ') flag = '/' if os.path.isdir (f) else ' print ('%10d %s %s%s ' % (Fsize, mtime, f, flag))
run Result:
Size last Modified Name------------------------------------------------------------469 2018-07-23 18:25 dir. PY 769 2018-07-23 17:48 file.py 186 2018-07-23 18:14 stringio.py one 2018-07-23 17:31 a.txt 12 288 2018-07-23 18:25. dir.py.swp
Python Operations files and directories: OS