Python Operations files and directories: OS

Source: Internet
Author: User
Tags posix

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.