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