Python (vii) OS module

Source: Internet
Author: User

File system features: OS module

When Python is programmed, it often deals with files and directories, which is not the OS module. The OS module contains common operating system features, regardless of the specific platform. The following lists the commonly used commands:

Directory:

Os.mkdir ('/tmp/dir1 ')

Os.makedirs (): Create a multilevel directory

Os.rmdir ()

Os.removedirs ()

OS.GETCWD ()

Os.chdir (' home ')

Os.root () Sets the current process root directory

Os.listdir ()

File:

Os.stat ('/home/zdjiang/get-pip.py ')

Os.fifo ()

Os.mknod (): Create a Device file

Os.remove (): Delete file

Os.unlink ():

Os.rename ()

Os.symlink ()

Os.utime (): Update file timestamp

Os.tmpfile (): Create and open a new temporary file (w+b)

Os.walk (): Equivalent to Tree

Access rights:

Os.acess (): Verify that a user has permission to a file

Os.chmod ()

Os.chown ()

Os.umask ()

File Descriptor:

Os.open (): The open of the underlying operating system

Os.read ()

Os.write ()

Device files:

Os.makedev (): Create device

Os.major ()

Os.minor ()

Os Sub-module: Os.path

Root file path correlation

Os.path.basename ()

Os.path.dirname ()

Os.path.join ()

Os.path.split (): Returns DirName (), basename () tuple

Splitext (): Return (filename,extension) tuple, ext extension

File information:

Os.path.getatime ()

Os.path.getctime ()

Os.path.getmtime ()

Os.path.getsize ()

Inquire:

Os.path.exists (): Determine if the file exists

Os.path.isabs (): Determines whether the specified path is an absolute path

Os.path.isdir (): Whether it is a directory

Os.path.isfile (): Whether it is a file

Os.path.link (): Whether it is a symbolic link

Os.path.ismount (): Whether it is a mount point

Os.path.samefile (): Two paths pointing to the same file

Practice:

Determine whether the file exists, the existence is open, let the user through the keyboard repeatedly input multiple lines of data, append to the file.

#!/usr/bin#import osimport os.pathfilename = '/tmp/test.txt ' if Os.path.isfile (filename):    f1 = open (filename, ' A + ') else:    f1 = open (filename, ' w+ ') while True: line    = Raw_input (' Enter something> ')    if line = = ' Q ' or line = = ' Q Uit ':        break    f1.write (line+ ' \ n ') F1.close ()

Python (vii) OS module

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.